stringbuilder转换成string
时间: 2023-05-01 09:01:28 浏览: 112
string,stringbuffer,stringbuilder
可以使用 `ToString()` 方法将 StringBuilder 对象转换为字符串。例如:
```
StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.Append("world!");
string s = sb.ToString();
Console.WriteLine(s);
```
也可以直接将 StringBuilder 对象强制转换为字符串,例如:
```
StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.Append("world!");
string s = (string)sb;
Console.WriteLine(s);
```
或者用implicit operator
```
StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.Append("world!");
string s = sb;
Console.WriteLine(s);
```
这3种方式都能将 StringBuilder 转换为字符串,请根据自己的需求选择。
阅读全文