一、System.String
string
等价于 System.String
。
string
是 System.String
的别名。
声明:
// 仅仅是一个声明,内存中并没有分配用来存储值的空间。
string a1 = null;
// 等价于 "",以新建字符串长度为零的 String 对象,可以减少 NullReferenceException 发生的可能性。
string a1 = String.Empty;
// 在内存中分配了一个连续性的不可变的储存空间。
System.String greeting = "Hello World!";
// 长字符串字面量拆分为较短的字符串,从而提高源代码的可读性。
// 以下代码将较短的字符串连接起来,以创建长字符串字面量。
// 在编译时将这些部分连接成一个字符串。 无论涉及到多少个字符串,均不产生运行时性能开销。
string text = "Historically, the world of data " +
"and the world of objects have not been well " +
"integrated. Programmers work in C# or Visual " +
"Basic and also in SQL or XQuery. On the one " +
"side are concepts such as classes, objects, " +
"fields, inheritance, and .NET Framework APIs. " +
"On the other side are tables, columns, rows, " +
"nodes, and separate languages for dealing with " +
"them. ";
System.String
是不可变类型。 也就是说,出现的用于修改对象的每个操作 System.String
实际上都会创建一个新的字符串。
如下举例:
// 初始化 s1
// 内存为 s1 分配了存放字符的空间
string s1 = "A string is more ";
// 初始化 s2
// 内存为 s2 分配了存放字符的空间
string s2 = "than the sum of its chars.";
// 改变 s1 的值:
// 内存为 s1 另外重新分配了新值的空间
// 并指向新空间的地址,所以 s1 有了新的值
// 无用的s1旧值及空间等待GC垃圾回收
s1 += s2;
System.Console.WriteLine(s1);
以上,假设更多的+或+=等的多次赋值s1,那么 内存在不停的开辟新的空间存放新值赋予s1 ... ... 每次再需要GC分配、压缩、清理... ...
对于此不可变类型,若执行大量字符串操作的例程 (例如在循环中多次修改字符串的应用程序) ,重复修改字符串可能会显著降低性能。
二、System.Text.StringBuilder
System.Text.StringBuilder
对象在缓冲区操作,是一个可变字符串类,缓冲区可随时增加或减少或删除或替换等的字符串的长度。
StringBuilder strb = new StringBuilder();
strb.Append('*', 10).Append(" Adding Text to a StringBuilder Object ").Append('*', 10);
strb.AppendLine("\n");
strb.AppendLine("Some code points and their corresponding characters:");
for (int ctr = 50; ctr <= 60; ctr++) {
strb.AppendFormat("{0,12:X4} {1,12}", ctr,Convert.ToChar(ctr));
strb.AppendLine();
}
三、适用场景
\ | string、String |
StringBuilder |
对比 |
连续性的不可变的内存空间长度; 每次开辟新内存空间,存放变更后的值,原空间等待释放; 借助IndexOf、Substring等进行字符搜索。 |
缓冲区可伸缩的空间长度; 原空间中维护字符长度,不创建新空间; (Chars[])字符搜索方式复杂。 |
场景 |
字符更改次数很少; 固定数量的拼接字符串联操作; 大量的字符串搜索。 |
大量的未知的不确定的更改次数; 字符串非搜索场景。 |