使用Remove()方法可按索引删除一系列字符。
假设您需要从以下字符串中删除最后5个字符-
StringBuilder myStr = new StringBuilder("Framework");
为此,将Remove()方法设置为-
str.Remove(3, 4);
以下是完整的代码-
using System; using System.Text; public class Program { public static void Main() { StringBuilder myStr = new StringBuilder("Framework"); Console.WriteLine("Initial String: " + myStr); // removing four characters Console.Write("New string: "); myStr.Remove(5, 4); Console.WriteLine(myStr); } }
输出结果
Initial String: Framework New string: Frame