Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
VB
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

Is there any easier way than my above code to accomplish this task?
Posted
Updated 26-Jan-10 20:05pm
v3

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


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.
 
Share this answer
 
v2
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):
VB.NET
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900