文本框中的多个替换文本数据 [英] Multiple Replace Text Data in Textbox

查看:90
本文介绍了文本框中的多个替换文本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用小型应用程序.它有两个文本框.我将输入数据放入textbox1,将输出数据放入textbox2.我要替换多个字符串,其中每个要替换的字符串都有一个要替换的唯一字符串,如下所示:

I''m using small application. It has two textboxes. I''m putting input data in textbox1 and output data in textbox2. I want to replace multiple strings, where each string to replace has a unique string to be replaced with, as in the following:

"KB" to "kilobyte"
"MB" to "megabyte"
"GB" to "gigabyte"
"TB" to "terabyte"



为此,我使用以下代码:



For this I am using this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = TextBox1.Text

        Dim i As Integer = 0
        Do While i < 1000
            i = i + 1
            If InStr(TextBox2.Text, "kb") Then
                TextBox2.Text = Replace(TextBox2.Text, "kb", "kilobyte")
            ElseIf InStr(TextBox2.Text, "mb") Then
                TextBox2.Text = Replace(TextBox2.Text, "mb", "megabyte")
            ElseIf InStr(TextBox2.Text, "gb") Then
                TextBox2.Text = Replace(TextBox2.Text, "gb", "gigabyte")
            ElseIf InStr(TextBox2.Text, "tb") Then
                TextBox2.Text = Replace(TextBox2.Text, "tb", "terabyte")
 
           End If
        Loop

    End Sub


是否有比我上面的代码更简单的方法来完成此任务?

推荐答案

是的.都是浪费时间.另外,如果存在多个值,则您的代码仅适用于这些值之一.只需将替换调用放在一个块中即可.摆脱循环.
Yes. It''s all a waste of time. Plus, your code just will work for one of these values, if more than one is there. Just put the Replace calls in a block. Get rid of the loop.
TextBox2.Text = Replace(TextBox2.Text, "kb", "kilobyte")
TextBox2.Text = Replace(TextBox2.Text, "mb", "megabyte")
TextBox2.Text = Replace(TextBox2.Text, "gb", "gigabyte")
TextBox2.Text = Replace(TextBox2.Text, "tb", "terabyte")



这就是您所需要的.另外,任何不带点的VB方法(因此InStr,而不是TextBox2.Text.Contains)都不要使用.之所以剩下这些,是因为VB6用户被认为太愚蠢,无法学习.NET.



That''s all you need. Also, any VB method that has no dot in it ( so InStr, instead of TextBox2.Text.Contains ), NEVER use. That''s stuff left in because VB6 users were presumed to be too stupid to learn .NET.


您可以将正则表达式与匹配评估器一起使用,并与字典(字典)结合使用会比您目前使用的技术提高性能):
You could use a regular expression with a match evaluator in combination with a dictionary (the dictionary will improve performance over the technique you are currently using):
Public Class Form1
    Private items As New Dictionary(Of String, String)
    Public Sub DoIt()
        items("x") = "the x"
        items("y") = "the y"
        items("z") = "the z"
        Dim pattern As String = "x|y|z"
        Dim reggy As New System.Text.RegularExpressions.Regex(pattern, _
            System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        Dim input As String = "The x saw the y and z."
        Dim output As String = reggy.Replace(input, _
            New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceUnit))
    End Sub
    Private Function ReplaceUnit(ByVal match As _
        System.Text.RegularExpressions.Match) As String
        Return items(match.Value.ToLower())
    End Function
End Class


这篇关于文本框中的多个替换文本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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