How to replace text in a string

后端 未结 2 1084
暖寄归人
暖寄归人 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: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.

提交回复
热议问题