How to replace letters in a textbox on time

Viewed 125

I want to replace every letter or number in a textbox using VB.NET. This was my first try, but it only replaces one letter at a time:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Select Case True
    Case TextBox1.Text.Contains("a")
        TextBox1.Text = TextBox1.Text.Replace("a", "h")
    Case TextBox1.Text.Contains("b")
        TextBox1.Text = TextBox1.Text.Replace("b", "o")
    Case TextBox1.Text.Contains("c")
        TextBox1.Text = TextBox1.Text.Replace("c", "t")
    Case TextBox1.Text.Contains("d")
        TextBox1.Text = TextBox1.Text.Replace("d", "e")
    Case TextBox1.Text.Contains("e")
        TextBox1.Text = TextBox1.Text.Replace("e", "i")
    Case TextBox1.Text.Contains("f")
        TextBox1.Text = TextBox1.Text.Replace("f", "a")
    Case TextBox1.Text.Contains("g")
        TextBox1.Text = TextBox1.Text.Replace("g", "j")
    Case TextBox1.Text.Contains("h")
        TextBox1.Text = TextBox1.Text.Replace("h", "f")
    Case TextBox1.Text.Contains("i")
        TextBox1.Text = TextBox1.Text.Replace("i", "k")
    Case TextBox1.Text.Contains("j")
        TextBox1.Text = TextBox1.Text.Replace("j", "b")
    Case TextBox1.Text.Contains("k")
        TextBox1.Text = TextBox1.Text.Replace("k", "n")
    Case TextBox1.Text.Contains("l")
        TextBox1.Text = TextBox1.Text.Replace("l", "r")
    Case TextBox1.Text.Contains("m")
        TextBox1.Text = TextBox1.Text.Replace("m", "d")
    Case TextBox1.Text.Contains("n")
        TextBox1.Text = TextBox1.Text.Replace("n", "s")
    Case TextBox1.Text.Contains("o")
        TextBox1.Text = TextBox1.Text.Replace("o", "u")
    Case TextBox1.Text.Contains("p")
        TextBox1.Text = TextBox1.Text.Replace("p", "g")
    Case TextBox1.Text.Contains("q")
        TextBox1.Text = TextBox1.Text.Replace("q", "w")
    Case TextBox1.Text.Contains("r")
        TextBox1.Text = TextBox1.Text.Replace("r", "m")
    Case TextBox1.Text.Contains("s")
        TextBox1.Text = TextBox1.Text.Replace("s", "q")
    Case TextBox1.Text.Contains("t")
        TextBox1.Text = TextBox1.Text.Replace("t", "x")
    Case TextBox1.Text.Contains("u")
        TextBox1.Text = TextBox1.Text.Replace("u", "c")
    Case TextBox1.Text.Contains("v")
        TextBox1.Text = TextBox1.Text.Replace("v", "y")
    Case TextBox1.Text.Contains("w")
        TextBox1.Text = TextBox1.Text.Replace("w", "z")
    Case TextBox1.Text.Contains("x")
        TextBox1.Text = TextBox1.Text.Replace("x", "l")
    Case TextBox1.Text.Contains("y")
        TextBox1.Text = TextBox1.Text.Replace("y", "v")
    Case TextBox1.Text.Contains("z")
        TextBox1.Text = TextBox1.Text.Replace("z", "p")
end select
end sub

This isn't what I want so I tried this:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

If TextBox1.Text.Contains("a") Then
    TextBox1.Text = TextBox1.Text.Replace("a", "h")
End If

If TextBox1.Text.Contains("b") Then
    TextBox1.Text = TextBox1.Text.Replace("b", "o")
End If

If TextBox1.Text.Contains("c") Then
    TextBox1.Text = TextBox1.Text.Replace("c", "t")
End If

If TextBox1.Text.Contains("d") Then
    TextBox1.Text = TextBox1.Text.Replace("d", "e")
End If

If TextBox1.Text.Contains("e") Then
    TextBox1.Text = TextBox1.Text.Replace("e", "i")
End If

If TextBox1.Text.Contains("f") Then
    TextBox1.Text = TextBox1.Text.Replace("f", "a")
End If

If TextBox1.Text.Contains("g") Then
    TextBox1.Text = TextBox1.Text.Replace("g", "j")
End If

If TextBox1.Text.Contains("h") Then
    TextBox1.Text = TextBox1.Text.Replace("h", "f")
End If

If TextBox1.Text.Contains("i") Then
    TextBox1.Text = TextBox1.Text.Replace("i", "k")
End If

If TextBox1.Text.Contains("j") Then
    TextBox1.Text = TextBox1.Text.Replace("j", "b")
End If

If TextBox1.Text.Contains("k") Then
    TextBox1.Text = TextBox1.Text.Replace("k", "n")
End If

If TextBox1.Text.Contains("l") Then
    TextBox1.Text = TextBox1.Text.Replace("l", "r")
End If

If TextBox1.Text.Contains("m") Then
    TextBox1.Text = TextBox1.Text.Replace("m", "d")

End If

If TextBox1.Text.Contains("n") Then
    TextBox1.Text = TextBox1.Text.Replace("n", "s")
End If

If TextBox1.Text.Contains("o") Then
    TextBox1.Text = TextBox1.Text.Replace("o", "u")
End If

If TextBox1.Text.Contains("p") Then
    TextBox1.Text = TextBox1.Text.Replace("p", "g")
End If

If TextBox1.Text.Contains("q") Then
    TextBox1.Text = TextBox1.Text.Replace("q", "w")
End If

If TextBox1.Text.Contains("r") Then
    TextBox1.Text = TextBox1.Text.Replace("r", "m")
End If

If TextBox1.Text.Contains("s") Then
    TextBox1.Text = TextBox1.Text.Replace("s", "q")
End If

If TextBox1.Text.Contains("t") Then
    TextBox1.Text = TextBox1.Text.Replace("t", "x")
End If

If TextBox1.Text.Contains("u") Then
    TextBox1.Text = TextBox1.Text.Replace("u", "c")
End If

If TextBox1.Text.Contains("v") Then
    TextBox1.Text = TextBox1.Text.Replace("v", "y")
End If

If TextBox1.Text.Contains("w") Then
    TextBox1.Text = TextBox1.Text.Replace("w", "z")
End If

If TextBox1.Text.Contains("x") Then
    TextBox1.Text = TextBox1.Text.Replace("x", "l")
End If

If TextBox1.Text.Contains("y") Then
    TextBox1.Text = TextBox1.Text.Replace("y", "v")
End If

If TextBox1.Text.Contains("z") Then
    TextBox1.Text = TextBox1.Text.Replace("z", "p")
End If
end sub

It doesn't work either, just with a letter at a time.

I want to be able to write, for example, "bike", in a textbox and it replaces the text in the same textbox (or another textbox) to, in this case, "pawm", but I can't see where the problem is.

3 Answers

Replacing the whole text doesn't work, as you have found out, because a letter can be replaced more than once. For example, the "b" is replaced with a "p" but later on you replace the "p" with an "h".

You want to do replacements character by character. Here is an example:

Imports System.Collections.Generic
Imports System.Linq

Dim replacement = New Dictionary(Of Char, Char) From
{
    {"b"c, "p"c},
    {"i"c, "a"c},
    {"k"c, "w"c},
    {"e"c, "m"c}
}

Dim word = "bike"

'For each character, we select the replacement letter
Dim letters = word.Select(Function(c) replacement(c)).ToArray()

'Construct a new string with the replaced letters
Dim newWord = New String(letters)

Imagine that the cipher was just a->b, b->c, c->d. Now, if the word is "abc", then we change all the a->b to get "bbc", then all the b->c to get "ccc" and then all the c->d to get "ddd". This is not what we wanted! Instead, we need to make a new string of letters, so have an "a" that we know ends up as "b": the new string is "b" so far. Then we look at the "b" in "abc" and we know it will become "c", so we append it to the new string to get "bc", and finally we look at the "c" in "abc", translate it to "d", append it to the new string and get "bcd". This is what we want, so we have found a method to do the enciphering.

So, we now need a way to look up the translation from the letters the user enters to the enciphered letters.

If we have a string of the letters in the alphabet in order, we can look up the position of a letter in that string by using the String.IndexOf function. So, if we ask it for the index of, say, "c" in "abcdefghijklmnopqrstuvwxyz" it will give us the number 2. It is 2 because it starts counting at zero instead of one. Now, if we have a string with the enciphered characters, we can get it to look up the character in the position we specify, so we look at the character with index 2 in the string "ypsvmdgjatwnqzkhebxurolifc" and we get "s".

Now, it is better to put little parts of code in their own methods (it means you can concentrate on a smaller piece of code if something needs correcting or changing), in this case it would be a good idea to make a function which you give the string you want enciphered and it returns the enciphered string.

So, putting those parts together, I got:

Function Encipher(s As String) As String
    Dim normal = "abcdefghijklmnopqrstuvwxyz"
    Dim cipher = "ypsvmdgjatwnqzkhebxurolifc"

    ' Make the uppercase versions too
    normal = normal & normal.ToUpper()
    cipher = cipher & cipher.ToUpper()

    Dim newString = ""

    For Each c In s
        Dim arrayIndex = normal.IndexOf(c)
        If arrayIndex >= 0 Then
            Dim cipherChar = cipher.Chars(arrayIndex)
            newString &= cipherChar
        Else
            newString &= c
        End If
    Next

    Return newString

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox1.Text = Encipher(TextBox1.Text)

End Sub

The If arrayIndex >= 0 Then part checks if the character was actually found, if not then the Else part appends the unaltered character.

this totally works

Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()

        For Each character As Char In RichTextBox1.Text
            If character = "a"c Then
                newString.Append(Chr(104))
            ElseIf character = "b"c Then
                newString.Append(Chr(111))
            ElseIf character = "c"c Then
                newString.Append(Chr(116))
            ElseIf character = "d"c Then
                newString.Append(Chr(101))
            ElseIf character = "e"c Then
                newString.Append(Chr(105))
            ElseIf character = "f"c Then
                newString.Append(Chr(97))
            ElseIf character = "g"c Then
                newString.Append(Chr(108))
            ElseIf character = "h"c Then
                newString.Append(Chr(102))
            ElseIf character = "i"c Then
                newString.Append(Chr(107))
            ElseIf character = "j"c Then
                newString.Append(Chr(98))
            ElseIf character = "k"c Then
                newString.Append(Chr(110))
            ElseIf character = "l"c Then
                newString.Append(Chr(114))
            ElseIf character = "m"c Then
                newString.Append(Chr(100))
            ElseIf character = "n"c Then
                newString.Append(Chr(115))
            ElseIf character = "o"c Then
                newString.Append(Chr(117))
            ElseIf character = "p"c Then
                newString.Append(Chr(103))
            ElseIf character = "q"c Then
                newString.Append(Chr(119))
            ElseIf character = "r"c Then
                newString.Append(Chr(109))
            ElseIf character = "s"c Then
                newString.Append(Chr(113))
            ElseIf character = "t"c Then
                newString.Append(Chr(120))
            ElseIf character = "u"c Then
                newString.Append(Chr(99))
            ElseIf character = "v"c Then
                newString.Append(Chr(121))
            ElseIf character = "w"c Then
                newString.Append(Chr(122))
            ElseIf character = "x"c Then
                newString.Append(Chr(108))
            ElseIf character = "y"c Then
                newString.Append(Chr(118))
            ElseIf character = "z"c Then
                newString.Append(Chr(112))
            ElseIf character = " "c Then
                newString.Append(Chr(32))
            Else
                newString.Append(Chr(Asc(character) + 2))
            End If
        Next

        RichTextBox1.Text = newString.ToString()
Related