string is immutable and stringbuilder is mutable

前端 未结 8 1306
轻奢々
轻奢々 2020-12-06 08:38

can any one explain with examples


Related Discussion: Most efficient way to concatenate strings?

相关标签:
8条回答
  • 2020-12-06 09:31

    String is immutable ,,

    That means we cannot change the string once declared .

    String builder is mutable

    This is the variety operation can be performed.we can change string ..

    0 讨论(0)
  • 2020-12-06 09:37

    You cannot edit the value of a string, each method of the string object returns a new string instead of altering the original. StringBuilder on the other hand can alter it's content (adding new strings for example).

    string original = "the brown fox jumped over the lazy dog";
    string altered = original.Insert(original.IndexOf("fox"), "cat");
    // altered = the brown cat jumped over the lazy dog
    

    You cannot change the content of the original string unless you create a new string, or re-reference the instance to another string object.

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