字符i在字符串str最后一次出现的位置是:13
十、字符串的追加
在使用System.String类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的String对象相关的系统开销就可能非常高。为了解决这个问题,C#提供了一个类StringBuilder。
使用StringBuilder类时,首先要引入System.Text命名空间,然后通过new关键字对其进行初始化。StringBuilder类的方法使用和String类的方法使用是一样的。
例十、实现对字符串str的追加
?
- <span style="font-size:18px;">using System; using System.Collections.Generic;
- using System.Linq; using System.Text;
- using System.Threading.Tasks;
- namespace 字符串 {
- class Program {
- static void Main(string[] args) {
- StringBuilder str =new StringBuilder( "Hellow World!"); Console.WriteLine("---Append---");
- str.Append("What a beautiful day"); Console.WriteLine("追加后的字符串为:{0}",str);
- Console.ReadLine(); }
- } }</span>
输出的结果为:---Append---
追加后的字符串为:Hellow World! What a beautiful day
补充:转义字符
转义字符具有特定的含义,不同于字符原有的意义的字符。在C#语言中,转义字符是指“”,主要用来表示那些用一般字符不方便表示的控制代码。
对于转义字符的输出C#语言有着特殊的格式:
- <span style="font-size:18px;">using System; using System.Collections.Generic;










