StringBuilder.Chars []属性在此实例中获取或设置字符在指定字符位置。
语法如下-
public char this[int index] { get; set; }
在上方,索引参数是字符的位置。
现在让我们看一个例子-
using System; using System.Text; public class Demo { public static void Main() { StringBuilder strBuilder = new StringBuilder("ghgh78hkjj"); int num = 0; for (int i = 0; i < strBuilder.Length; i++) { char c = strBuilder[i]; if (Char.IsDigit(c)) num++; } Console.WriteLine("String = "+strBuilder); Console.WriteLine("Numbers in the string = "+num); } }
输出结果
这将产生以下输出-
String = ghgh78hkjj Numbers in the string = 2
现在让我们来看另一个例子-
using System; using System.Text; public class Demo { public static void Main() { StringBuilder strBuilder = new StringBuilder("ghgh78hkjj"); char c = strBuilder[3]; Console.WriteLine("String = "+strBuilder); Console.WriteLine("Character = "+c); } }
输出结果
这将产生以下输出-
String = ghgh78hkjj Character = h