何我反向文字

时间:2016-12-08 18:44:44

标签: vb.net

我正在我的项目中工作,它的一个功能是只反转文本(数字应该保留为它)。例如,文本这是一个测试文件变为 elif tset a si sihT 。另一个包含数字 USA 123 UAE 的例子应该成为 EAU 123 ASU 我编写了以下程序但结果不正确,特别是对于包含数字的行(当同一行中的英文文本用数字分隔时会出现问题)。例如,我得到 USA 123 UAE 而不是正确的结果 EAU 123 ASU 。句子科威特伊拉克784巴林应该是 niarhaB 784 qarI tiawuK 但是我得到了原来的那个。所以任何帮助或建议 这是我的代码

FileName = "C:\Users\PC\Desktop\test.txt"
Dim AllTextFile As String = File.ReadAllText(FileName)
Dim objReader As New System.IO.StreamReader(FileName)
Dim seperatedWordsArray As String()
Dim allTextLines As String = ""
Dim tempTextLine As String = ""
Dim englishSentence As String = ""
Dim someSentences As New List(Of String)
Do While objReader.Peek() <> -1
    TextLine = objReader.ReadLine()
    tempTextLine = TextLine
    englishSentence = ""
    seperatedWordsArray = Regex.Split(TextLine, " "c)
    For Each word As String In seperatedWordsArray
        If (Regex.IsMatch(word, "^[a-zA-Z,.:]*$")) Then
            englishSentence = englishSentence & word & " "
        End If
    Next

    englishSentence = englishSentence.TrimEnd(" ") 'Remove the last space that added by the previous sentence englishSentence = englishSentence & word & " "
    If (englishSentence.Length > 0) Then
        tempTextLine = tempTextLine.Replace(englishSentence, StrReverse(englishSentence))
    End If
    allTextLines = allTextLines & tempTextLine & vbNewLine
Loop
TextBox2.Text = allTextLines</pre>

我使用以下文字尝试了我的程序:

这是一个测试文件

美国123阿联酋

特朗普大厦NY 667

123 abcdef ABCDEF

科威特伊拉克784巴林

句子是表达陈述,问题,命令或感叹号的一组词语

111句454种不同类型777:

声明,问题类型,命令类型,感叹类型

1 个答案:

答案 0 :(得分:0)

    Public Sub Main()
        Dim sTest As String = "Kuwait Iraq 784 Bahrain"
        Dim sRevLine As String = ""
        For Each sItem as String in sTest.Split(" ")
            If Microsoft.VisualBasic.IsNumeric(sItem) Then
                sRevLine = sItem + " " + sRevLine
            Else
                sRevLine = strReverse(sItem) + " " + sRevLine
            End If
        Next
        sRevLine = sRevLine.Trim(" ")
        Console.WriteLine(sRevLine)
    End Sub

    public function strReverse(sIn as String) as String
        Dim cA = sIn.ToCharArray()
        Array.Reverse(cA)
        return New String(cA)
    end function