String in C# - string vs StringBuilder

string

string is an alias for System.String .
It should be noted that the string created by string is actually an immutable data type. Once a string object is initialized, the string cannot change its contents.
for:

string s = "www";
s = s + ".baidu.com";

In fact, a new string is created, the contents of the existing string are copied into the new string, and a reference to the new string is assigned to the string object.
The original old string will be reclaimed by GC because there is no reference.

Common method

  • CompareTo() : Compares the contents of two strings for equality.
  • Replace(): Replaces a given character or string in a string with another character or string.
  • Split(): Splits the string into an array of strings at the occurrence of the given character.
  • SubString(): Retrieves the substring at the given position in the string.
  • ToLower(): Convert the string to lowercase.
  • ToUpper(): Convert the string to uppercase.
  • Trim(): Remove leading and trailing whitespace.
  • Concat(): Combine strings.
  • CopyTo(): ​​Copies the specified characters in the string to one.
  • Format(): Format string.
  • IndexOf(): Gets the position of the first occurrence of a given string or character in a string.
  • IndexOfAny(): Returns the earliest subscript position of any character in the defined array.
  • Insert(): Inserts a string instance into another string instance at the specified index.
  • Join(): Combines arrays of strings, creating a new string.
    The specific use method and parameters are omitted.

StringBuilder

First, StringBuilder is in the namespace: System.Text .

  • How to initialize:
//利用构造函数创建StringBuilder
StringBuilder sb = new StringBuilder("www.baidu.com");

//初始一个空的StringBuilder对象,占有20个字符大小的空间
StringBuilder sb = new StringBuilder(20);

//初始化字符串并占有100个字符大小的空间
StringBuilder sb = new StringBuilder("www.baidu.com", 100);
  • Add new string: ( Append() method )
StringBuilder sb = new StringBuilder("www.baidu.com", 100);
sb.Append("/xxx.html");

//利用string实现
string s = "www.baidu.com";
s = s + "/xxx.html";

Although the results of the two are the same, when we need to perform frequent deletion and addition operations on a string, using StringBuilder will be more efficient.

For StringBuilder, when the capacity is not enough, it will re-apply for a space with twice the original capacity, then copy the original content and add new content, and the original old space will be released. When defining StringBuilder, it is also possible not to define the capacity.

Other methods of StringBuilder

  • Insert()
    appends a string in a specific format.
StringBuilder sb = new StringBuilder("www.baidu.com");
sb.Insert(0, "http://");    //0表示插入的索引(位置)

输出为:
http://www.baidu.com
  • Remove()
    removes characters from the current string.
StringBuilder sb = new StringBuilder("www.baidu.com");
sb.Remove(0,3);     //第一个参数为移除的初始索引,第二个参数为移除的字符的长度

输出为:
baidu.com
  • Replace()
    In the current string, replace all a string or character with another string or character.
StringBuilder sb = new StringBuilder("www.baidu.com");
sb.Replace(".","");     //字符串替换字符串

输出为:
wwwbaiducom

StringBuilder sb = new StringBuilder("www.baidu.com");
sb.Replace('w','e');    //字符替换字符

输出为:
eee.baidu.com
  • ToString()
    extracts the string stored in the current StringBuilder into an immutable string.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324852364&siteId=291194637