Multiple Substitution in VB.NET

I am making a program, i.e. a converter script. I tried the Replace command. TextBox1.Text.Replace("Hi", "Hello").Replace("Hello", "HI") But that doesn't work. It does not replace the second time correctly.

Please, help...

+5
source share
2 answers

The Replace () method does not actually change the contents of the string. Therefore, you need to assign a new value.

Example:

someString = "First Example"

someString.Replace("First", "Second")

// someString is still "First Example"

newString = "Hello World".Replace("Hello", "Hi")

// newString is now "Hi World"

Some examples: http://www.dotnetperls.com/replace-vbnet

Update:

From your recent comment, it seems like you want this:

TextBox1.Text.Replace("Hi", "temp").Replace("Hello", "HI").Replace("temp", "Hello")

. . , "" "" "" "", .

+4
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = TextBox1.Text.Replace("Hi", "Hello").Replace("Hello", "HI")
End Sub

, , ,

+1

All Articles