如何使用 vb.net 替换文本框上的字母 [英] How to replace letters on a textbox using vb.net

查看:43
本文介绍了如何使用 vb.net 替换文本框上的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 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.

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

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.

推荐答案

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

To expand upon Joe's answer and pseudo code, a simple For Each loop 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.

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

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.).

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

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).

示例代码来说明这一点:

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

非常简单.我使用了 StringBuilder 因为它可以更高效一点,这取决于字符串的长度.替代方案:

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

Dim newString As String

newString = newString + Chr(character) 

会为循环中的每次迭代生成一个新的 newString 副本 - 所以如果你有一个 10 个字符的字符串,你会得到 10 个 newString - 每个循环一个.

Would result in a new copy of newString being 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.

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

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

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

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.

这篇关于如何使用 vb.net 替换文本框上的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆