C# 中的 StringBuilder.Chars[] 属性
StringBuilder.Chars[] 属性获取或设置本实例中指定字符位置的字符。
语法
语法如下 -
public char this[int index] { get; set; }
上述代码中,index 参数为此字符的位置。
示例
现在,让我们看一个示例 -
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
广告