VB - 替换字母

时间:2016-12-14 20:27:04

标签: vb.net substitution

所以我正在制作一个解密软件,允许用户输入一些文本然后他们可以换掉程序中的字母。例如,有一个下拉框,允许您将用户输入中的所有“O”交换为“W”。所以在输入“Stack overflow”中,输出将是“Stack wverflww”。

但是,我的问题是,当用户选择要更改的第二个字母时,已经交换了,这会导致问题。例如,在上面的第一个例子发生之后,如果用户然后想要将他们输入中的所有“W”改为“A”,则输出将是“stack averflaa”。但是,我正在寻找的代码是输出“Stack wverflwa”。因此,只有用户输入的原始“W”变为字母“A”。

我希望上述内容有道理。

有人建议使用二维数组来重新分配字母的新字母,我能够做到这一点,但我不知道如何将其放入我的代码中并使其正常工作。以下是我的代码,感谢能帮助我的任何人。

    Dim chooseLetter, replaceLetter, words2
    chooseLetter = selectLetterCombo.Text
    replaceLetter = replaceLetterCombo.Text
    words2 = UCase(textInputBox.Text)
    Dim replaceList As New List(Of String)
    For Each z In words2
        If z = chooseLetter Then
            replaceList.Add(replaceLetter)
        Else
            replaceList.Add(z)
        End If
    Next
    letterReplaceBox.Text = ""
    For Each f In replaceList
        letterReplaceBox.Text = letterReplaceBox.Text & f
    Next

注意:selectLetterCombo.Text是用户选择要替换的字母,而replaceLetterCombo.Text是用户选择要用来交换第一个选定字母的字母。此外,textInputBox.text是用户输入的文本。

谢谢!

3 个答案:

答案 0 :(得分:1)

您应该能够保留已更改字符的索引列表,并在进行其他更改之前进行检查。

'List to keep track of changed character index
Dim replacedCharsList As New List(Of Integer)'member variable

Dim chooseLetter, replaceLetter, words2
chooseLetter = selectLetterCombo.Text
replaceLetter = replaceLetterCombo.Text
words2 = UCase(textInputBox.Text)

Dim replaceList As New List(Of String)
Dim i As Integer
For i = 1 To Len(words2)
    'remove the for each and go with a straight for loop to keep track if the index
    If Mid(words2, i, 1) = chooseLetter Then
        'check to see if we have already replaced this character via the index position
        If replacedCharsList.Contains(i) = False Then
            'we have not changed this so add the replacement letter and update our index list
            replaceList.Add(replaceLetter)
            replacedCharsList.Add(i)
        Else
            'we have already changed this character so just add it as is
            replaceList.Add(Mid(words2, i, 1))
        End If
    Else
        replaceList.Add(Mid(words2, i, 1))
    End If
Next

letterReplaceBox.Text = ""
For Each f In replaceList
    letterReplaceBox.Text = letterReplaceBox.Text & f
Next

答案 1 :(得分:0)

我有 答案,但您真的不喜欢它:

Option Infer On
Option Strict On

Imports System.Text.RegularExpressions

Module Module1

    Dim swaps As New Dictionary(Of Char, Char)

    Function DoSwaps(originalText As String, swapLetters As Dictionary(Of Char, Char)) As String
        Dim newText As String = ""
        For Each c In originalText
            If swapLetters.ContainsKey(c) Then
                newText &= swapLetters(c)
            Else
                newText &= c
            End If
        Next

        Return newText

    End Function

    Sub Main()
        Console.Write("Enter the text to be altered: ")
        Dim t = Console.ReadLine()

        Dim exitNow = False

        Do
            Console.Write("Enter the letter to swap from and the letter to swap to, or a blank line to quit: ")
            Dim s = Console.ReadLine()
            If s.Trim().Length = 0 Then
                exitNow = True
            Else
                Dim parts = Regex.Matches(s, "([A-Za-z])")
                If parts.Count >= 2 Then
                    Dim letter1 = CChar(parts.Item(0).Value)
                    Dim letter2 = CChar(parts.Item(1).Value)

                    If swaps.ContainsKey(letter1) Then
                        swaps.Item(letter1) = letter2
                    Else
                        swaps.Add(letter1, letter2)
                    End If

                    Console.WriteLine(DoSwaps(t, swaps))

                End If
            End If

        Loop Until exitNow

    End Sub

End Module

...除非您想了解Dictionary class以了解其运作方式。我使用简单的regular expression来解析用户输入,但是如果您使用下拉菜单来选择字母,那么如果您进行探索,这只是奖励学习。

基本功能是您将原始字符串t保留在上面的代码中)并将转换(我将其命名为DoSwaps)应用于 每次,而不是之前转换过的字符串。

答案 2 :(得分:0)

这两个函数可以完成这项工作,虽然没有标点符号,只是空格。

Private Function EncryptText(str As String) As String
    Dim swapletters() As String = {"l", "s", "d", "f", "g", "h", "j", "k", "a", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "z", "x", "c", "v", "b", "n", "m"}
    Dim encryptedText As String = ""
    For Each letter As Char In str
        If letter = " "c Then
            encryptedText = encryptedText & " "
        Else
            Dim charactercode As Integer = Asc(letter) - 97
            encryptedText = encryptedText & swapletters(charactercode)
        End If
    Next
    Return encryptedText
End Function

Private Function DecryptText(str As String) As String
    Dim swapletters As New List(Of String) From {"l", "s", "d", "f", "g", "h", "j", "k", "a", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "z", "x", "c", "v", "b", "n", "m"}
    Dim decryptedText As String = ""
    For Each letter As Char In str
        If letter = " "c Then
            decryptedText = decryptedText & " "
        Else
            Dim character As String = Chr(swapletters.IndexOf(letter)  + 97)
            decryptedText = decryptedText & character
        End If
    Next
    Return decryptedText
End Function

要使用它们,请声明一个字符串以保存每个函数的返回值

Dim etext As String 
etext = EncryptText("disambiguation is the root of all evil")

导致etext成为" faplrsajxlzayt ap zkg oyyz yh lee gcae"

Dim dtext As String 
dtext = DecryptText("faplrsajxlzayt ap zkg oyyz yh lee gcae")

导致消除歧义是所有邪恶的根源"

相关问题