【问题标题】:Function convert cyrillic to latin函数将西里尔文转换为拉丁文
【发布时间】:2014-04-17 12:42:51
【问题描述】:

我正在尝试在 VB.net 中创建自定义将西里尔字母转换为拉丁文本的功能。我从来没有尝试过自定义函数,所以我不知道我做错了什么。我有一个问题,而且函数不起作用:对象引用未设置为对象的实例。

    Public Function ConvertCtoL(ByVal ctol As String) As String

    ctol = Replace(ctol, "Б", "B") 
    ctol = Replace(ctol, "б", "b")

**End Function** ' doesn't return a value on all code paths

由于我没有找到将西里尔文转换为拉丁文的解决方案,因此我计划创建一个函数,将每个字母从一个字母替换为另一个。

【问题讨论】:

    标签: vb.net replace converter cyrillic latin


    【解决方案1】:

    你需要Return ctol 告诉它返回什么值。

    也许研究“查找表”会帮助你做一个更简洁的函数。

    编辑:Lookup table 的维基百科条目应该是一个好的开始。

    这是一个简单的例子:

    Imports System.Text
    
    Module Module1
    
        Function ReverseAlphabet(s As String) As String
            Dim inputTable() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
            Dim outputTable() As Char = "ZYXWVUTSRQPONMLKJIHGFEDBCA".ToCharArray()
            Dim sb As New StringBuilder
    
            For Each c As Char In s
                Dim inputIndex = Array.IndexOf(inputTable, c)
                If inputIndex >= 0 Then
                    ' we found it - look up the value to convert it to.
                    Dim outputChar = outputTable(inputIndex)
                    sb.Append(outputChar)
                Else
                    ' we don't know what to do with it, so leave it as is.
                    sb.Append(c)
                End If
            Next
    
            Return sb.ToString()
    
        End Function
    
        Sub Main()
            Console.WriteLine(ReverseAlphabet("ABC4")) ' outputs "ZYX4"
            Console.ReadLine()
        End Sub
    
    End Module
    

    【讨论】:

    • 谢谢,你能提供一些关于查找表的链接吗,因为我对此一无所知。
    • @user3338345 我已经用一个链接和一个简单的例子编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多