How to replace text in a string

后端 未结 2 1083
暖寄归人
暖寄归人 2021-01-24 01:51

This is my code, I want to replace the word \"here\" with \"potato\" in the simplest way.

Private Sub btnreplace_Click(ByVal sender As System.Object, ByVal e As          


        
相关标签:
2条回答
  • 2021-01-24 02:18

    since its a particular word you can use simple text replacement other than using regex.replace()

    try this

    txttyping.Text = Replace(txttyping.Text, "here", "potato")
    

    here replace(sourcestring,findword,replaceword)

    0 讨论(0)
  • 2021-01-24 02:26

    Strings (text) are immutable. This means they cannot be changed directly, to alter one, a new string is created/returned.

    txttyping.Text = txttyping.Text.Replace("here", "potato)
    

    Replace() returns a new string which needs to be assigned. This is true for all the String methods which change the string: ToLower(), ToUpper(), Remove(), PadLeft(), Copy(), Remove(), TrimEnd().

    See MSDN String Class:

    A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.

    0 讨论(0)
提交回复
热议问题