替换每一封信

时间:2015-08-28 15:12:36

标签: vb.net string replace

代码应该将字母替换为图像链接,但它有时会取代例如:heyho to heoo,这就是问题所在。请帮帮我这个家伙!

    Imports System.Text.RegularExpressions

    Public Class Form1
        Dim DefURL = "http://www.roblox.com/-------------------------------" & _ 
                     "-----------------------------------------------------" & _
                     "---------------------------"

        Dim myString = ""
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub
        Sub Delay(ByVal dblsecs As Double)
            Const onesec As Double = 1.0# / (1440.0# * 60.0#)
            Dim dblWaitTil As Date
            Now.AddSeconds(onesec)
            dblWaitTil = Now.AddSeconds(onesec).AddSeconds(dblsecs)
            Do Until Now > dblWaitTil
                Application.DoEvents()

            Loop
        End Sub

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            myString = ""
            myString = TextBox1.Text.ToLower
            Dim sb As String
            For Each ch As Char In TextBox1.Text
                Dim s As String
                Console.WriteLine(ch)
                Select Case ch
                    Case "a"c : s = "http://www.roblox.com/--item?id=2273128"
                    Case "b"c : s = "http://www.roblox.com/--item?id=2273124"
                    Case "c"c : s = "http://www.roblox.com/--item?id=2273121"
                    Case "d"c : s = "http://www.roblox.com/--item?id=2273119"
                    Case "e"c : s = "http://www.roblox.com/--item?id=2273116"
                    Case "f"c : s = "http://www.roblox.com/--item?id=2273114"
                    Case "g"c : s = "http://www.roblox.com/--item?id=2273111"
                    Case "h"c : s = "http://www.roblox.com/--item?id=2273107"
                    Case "i"c : s = "http://www.roblox.com/--item?id=2273105"
                    Case "j"c : s = "http://www.roblox.com/--item?id=2273102"
                    Case "k"c : s = "http://www.roblox.com/--item?id=2273099"
                    Case "l"c : s = "http://www.roblox.com/--item?id=2273095"
                    Case "m"c : s = "http://www.roblox.com/--item?id=2273093"
                    Case "n"c : s = "http://www.roblox.com/--item?id=2273090"
                    Case "o"c : s = "http://www.roblox.com/--item?id=2273088"
                    Case "p"c : s = "http://www.roblox.com/--item?id=2273086"
                    Case "q"c : s = "http://www.roblox.com/--item?id=2273082"
                    Case "r"c : s = "http://www.roblox.com/--item?id=2273078"
                    Case "s"c : s = "http://www.roblox.com/--item?id=195452703"
                    Case "u"c : s = "http://www.roblox.com/--item?id=2273071"
                    Case "v"c : s = "http://www.roblox.com/--item?id=2273067"
                    Case "w"c : s = "http://www.roblox.com/--item?id=2273065"
                    Case "x"c : s = "http://www.roblox.com/--item?id=2273062"
                    Case "y"c : s = "http://www.roblox.com/--item?id=2273060"
                    Case "t"c : s = "http://www.roblox.com/--item?id=2273073"
                    Case "z"c : s = "http://www.roblox.com/--item?id=2273058"
                    Case " "c : s = "http://www.roblox.com/--item?id=269779634"
                End Select
                sb = sb & s
            Next
            My.Computer.Clipboard.SetText(sb.ToString)
        End Sub
    End Class

(请忽略这一点,因为编辑器不会让我提交,除非我添加更多文本。我只是想修复Umom Mommi的格式以使其符合格式标准。 - 谢谢 - 书呆子)

1 个答案:

答案 0 :(得分:2)

为避免替换已插入的字符串中的字符,可以遍历原始字符串中的所有字符,并使用替换文本构建新字符串。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sb As New System.Text.StringBuilder 
    For Each ch As Char In TextBox1.Text
        Dim s As String 
        Select Case ch
            Case "a"c: s = "http://www.roblox.com/--item?id=2273128 "
            Case "b"c: s = "http://www.roblox.com/--item?id=2273124 "
            Case "c"c: s = "http://www.roblox.com/--item?id=2273121 "
            Case Else: s = ch 
        End Select
        sb.Append(s)
    Next
    My.Computer.Clipboard.SetText(sb.ToString)
End Sub