如何使用 vb.net 替换文本框上的字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29736372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:02:56  来源:igfitidea点击:

How to replace letters on a textbox using vb.net

vb.net

提问by Milton Cardoso

I want to replace every letter or number on a textbox using vb.net this was my first try, but it only replaces one letter at a time

我想使用 vb.net 替换文本框中的每个字母或数字这是我的第一次尝试,但它一次只替换一个字母

 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", "c")
        Case TextBox1.Text.Contains("b")
            TextBox1.Text = TextBox1.Text.Replace("b", "d")
        Case TextBox1.Text.Contains("c")
            TextBox1.Text = TextBox1.Text.Replace("c", "e")
        Case TextBox1.Text.Contains("d")
            TextBox1.Text = TextBox1.Text.Replace("d", "f")
        Case TextBox1.Text.Contains("e")
            TextBox1.Text = TextBox1.Text.Replace("e", "g")
        Case TextBox1.Text.Contains("f")
            TextBox1.Text = TextBox1.Text.Replace("f", "h")
        Case TextBox1.Text.Contains("g")
            TextBox1.Text = TextBox1.Text.Replace("g", "i")
        Case TextBox1.Text.Contains("h")
            TextBox1.Text = TextBox1.Text.Replace("h", "j")
        Case TextBox1.Text.Contains("i")
            TextBox1.Text = TextBox1.Text.Replace("i", "k")
        Case TextBox1.Text.Contains("j")
            TextBox1.Text = TextBox1.Text.Replace("j", "l")
        Case TextBox1.Text.Contains("k")
            TextBox1.Text = TextBox1.Text.Replace("k", "m")
        Case TextBox1.Text.Contains("l")
            TextBox1.Text = TextBox1.Text.Replace("l", "n")
        Case TextBox1.Text.Contains("m")
            TextBox1.Text = TextBox1.Text.Replace("m", "o")
        Case TextBox1.Text.Contains("n")
            TextBox1.Text = TextBox1.Text.Replace("n", "p")
        Case TextBox1.Text.Contains("o")
            TextBox1.Text = TextBox1.Text.Replace("o", "q")
        Case TextBox1.Text.Contains("p")
            TextBox1.Text = TextBox1.Text.Replace("p", "r")
        Case TextBox1.Text.Contains("q")
            TextBox1.Text = TextBox1.Text.Replace("q", "s")
        Case TextBox1.Text.Contains("r")
            TextBox1.Text = TextBox1.Text.Replace("r", "t")
        Case TextBox1.Text.Contains("s")
            TextBox1.Text = TextBox1.Text.Replace("s", "u")
        Case TextBox1.Text.Contains("t")
            TextBox1.Text = TextBox1.Text.Replace("t", "v")
        Case TextBox1.Text.Contains("u")
            TextBox1.Text = TextBox1.Text.Replace("u", "w")
        Case TextBox1.Text.Contains("v")
            TextBox1.Text = TextBox1.Text.Replace("v", "x")
        Case TextBox1.Text.Contains("w")
            TextBox1.Text = TextBox1.Text.Replace("w", "y")
        Case TextBox1.Text.Contains("x")
            TextBox1.Text = TextBox1.Text.Replace("x", "z")
        Case TextBox1.Text.Contains("y")
            TextBox1.Text = TextBox1.Text.Replace("y", "a")
        Case TextBox1.Text.Contains("z")
            TextBox1.Text = TextBox1.Text.Replace("z", "b")

    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", "c")
    End If

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

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

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

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

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

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

    If TextBox1.Text.Contains("h") Then
        TextBox1.Text = TextBox1.Text.Replace("h", "j")
    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", "l")
    End If

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

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

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

    End If

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

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

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

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

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

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

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

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

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

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

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

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

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


End Sub

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

它也不起作用,一次只用一个字母。

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

我希望能够在文本框中书写,例如“自行车”,在这种情况下,它会替换同一文本框(或其他文本框)中的文本:“dkmg”,但我看不出问题出在哪里。

回答by Joe

What you must do is to:

你必须做的是:

  1. Loop through each character of the textbox one after the other
  2. Each time, you take one character, "process it" (replace it in your case), then you write it in a separate string, a bit like this (pseudo-code):

    new_string = ""
    for char in textbox.text:
        # do something with char, your 'if' routines would probably work
        new_string = new_string + char
    
  3. Then you assign the new string to the textbox:

    textbox.text = new_string
    
  1. 一个接一个循环遍历文本框的每个字符
  2. 每次,你取一个字符,“处理它”(在你的情况下替换它),然后将它写在一个单独的字符串中,有点像这样(伪代码):

    new_string = ""
    for char in textbox.text:
        # do something with char, your 'if' routines would probably work
        new_string = new_string + char
    
  3. 然后将新字符串分配给文本框:

    textbox.text = new_string
    

If you always want to add 2 letters to each letter, there's a way to treat each character as an integer. There's an example here(look at the bottom). Once you have this, you can simply add "2" to your char before printing it back to the string (some conversion might be needed, i'll let you figure that out):

如果您总是想为每个字母添加 2 个字母,则有一种方法可以将每个字符视为一个整数。这里有一个例子在这里(看看下图)。一旦你有了这个,你可以简单地在你的字符中添加“2”,然后再将它打印回字符串(可能需要一些转换,我会让你弄清楚):

    for char in textbox.text:
        new_string = new_string + (char + 2)

This mostly works, but you'll have to treat edge cases (y, z, Y, Z) yourself. You can use this chartfor reference.

这通常有效,但您必须自己处理边缘情况(y,z,Y,Z)。您可以使用此图表作为参考。

回答by Tim

To expand upon Joe's answer and pseudo code, a simple For Eachloop through the string will allow you to process one character at a time. The easiest way to do this is to use the ASCII value of the character to determine what to replace it with, but as Joe noted you have to handle the edge cases, since the ASCII table contains many symbols, not just letter, and they're in a specific order.

为了扩展 Joe 的答案和伪代码,For Each通过字符串的简单循环将允许您一次处理一个字符。最简单的方法是使用字符的 ASCII 值来确定用什么来替换它,但正如乔指出的那样,您必须处理边缘情况,因为 ASCII 表包含许多符号,而不仅仅是字母,而且它们重新按特定顺序。

In your posted code, you appear to be replacing each letter with the corresponding letter 2 spaces from the current letter's position (i.e., a = c, b = d, etc.).

在您发布的代码中,您似乎将每个字母替换为与当前字母位置相距 2 个空格的相应字母(即 a = c、b = d 等)。

The ASCII table uses 65-90 for A to Z, and 97-122 for a to z. Hence the edge cases for Y, Z, y and z - if you add 2 to Z, for example, you will get |, rather than B. This is where If statements can help (there are other ways to do it as well, like Select).

ASCII 表对 A 到 Z 使用 65-90,对 a 到 z 使用 97-122。因此,Y、Z、y 和 z 的边缘情况 - 例如,如果您将 2 添加到 Z,您将得到|,而不是 B。这就是 If 语句可以提供帮助的地方(还有其他方法可以做到,例如Select)。

Sample code to illustrate this:

示例代码来说明这一点:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim newString As StringBuilder = New StringBuilder()

    For Each character As Char In TextBox1.Text
        If character = "Y"c Then
            newString.Append(Chr(65))
        Else If character = "Z"c Then
            newString.Append(Chr(66))
        Else If character = "y"c Then
            newString.Append(Chr(97))
        Else If character = "z"c Then
            newString.Append(Chr(98))
        Else
            newString.Append(Chr(Asc(character) + 2))
        End If
    Next

    TextBox1.Text = newString.ToString()
End Sub

Pretty straightforward. I used StringBuilderas it can be a little more efficient, depending on the length of the string. The alternative:

很简单。我使用StringBuilder它可以更有效一点,具体取决于字符串的长度。替代方案:

Dim newString As String

newString = newString + Chr(character) 

Would result in a new copy of newStringbeing made for each iteration through the loop - so if you had a 10 character string, you'd wind up with 10 copies of newString- one for each loop.

将导致newString为循环中的每次迭代创建一个新副本- 因此,如果您有一个 10 个字符的字符串,您将得到 10 个副本newString- 每个循环一个。

The Asc function gets the ASCII value of the character, and the Chr function gets the character specified by the ASCII value. The lowercase cnext to the character (i.e., "A"c) simply indicates it's a Char, not a String.

Asc函数获取字符的ASCII值,Chr函数获取ASCII值指定的字符。c字符旁边的小写字母(即"A"c)仅表示它是 a Char,而不是 a String

Note that the above code will handle any ASCII value, so if you want to handle only characters, you'll need to do more checking in the loop. I'll leave that as an exercise for the readers.

请注意,上面的代码将处理任何 ASCII 值,因此如果您只想处理字符,则需要在循环中进行更多检查。我将把它留给读者作为练习。

回答by Craig Johnson

Here you go:

干得好:

    TextBox1.Text = (From c In TextBox1.Text.ToLower
                     Where c >= "a"c AndAlso c <= "z"c
                     Select Chr(97 + (Asc(c) - 95) Mod 26)).ToArray

回答by faleh

To change first letter to upper and the rest to lower after loss of focus:

失去焦点后将第一个字母更改为大写,将其余字母更改为小写:

 Private Sub CapsLock(TextCaps)
    '???? ??? ??? ??? ??? ???
    Dim fNameS As String = ""
    Dim liS As String = ""
    Dim lis2 As String = ""

    For i = 1 To Len(TextCaps)
        liS = Mid(TextCaps, i, 1)
        If i > 1 Then
            lis2 = Mid(TextCaps, (i - 1), 1)
        End If

        If i = 1 Or lis2 = " " Then

            liS = liS.ToUpper
            fNameS = fNameS & liS

        Else

            liS = liS.ToLower

            fNameS = fNameS & liS


        End If
    Next
    TextCaps2 = fNameS