C#中的StringBuilder.CopyTo()方法
C#中的StringBuilder.CopyTo()方法用于将字符从此实例的指定段复制到目标Char数组的指定段。
语法
语法如下-
public void CopyTo (int sourceIndex, char[] dest, int destIndex, int count);
在上面,参数sourceIndex是此实例中字符复制的起始位置。dest是要复制字符的数组,而destIndex是目标将复制字符的起始位置。count参数是要复制的字符数。
示例
现在让我们看一个例子-
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); char[] arr = new char[15]; strBuilder.CopyTo(3, arr, 2, 6); Console.WriteLine("\nCopied String in char array..."); Console.WriteLine(arr); } }
输出结果
String = ghgh78hkjj Character = h Copied String in char array... h78hkj
示例
现在让我们来看另一个例子-
using System; using System.Text; public class Demo { public static void Main() { StringBuilder strBuilder = new StringBuilder("JohnWick"); Console.WriteLine("String = "+strBuilder); char[] arr = new char[5] {'a', 'b', 'c', 'd', 'e'}; strBuilder.CopyTo(1, arr, 1, 3); Console.WriteLine("\nCopied String in char array..."); Console.WriteLine(arr); } }
输出结果
这将产生以下输出-
String = JohnWick Copied String in char array... aohne