StringBuilder。C# 中的复制方法
原文:https://www . geeksforgeeks . org/stringbuilder-copy to-method-in-c-sharp/
此方法用于将字符从该实例的指定段复制到目标字符数组的指定段。
语法:
public see copy to(int source index、char[] destination、int destinationIndex、int count);
参数:
- 源索引:这是这个实例中复制字符的起始位置。索引从零开始。
- 目的地:是复制字符的数组。
- 目的地索引:目的地中复制字符的起始位置。索引从零开始。
- 计数:是需要复制的字符数。
异常:
- 参数空异常:如果目的地为空。
- ArgumentOutOfRangeException:如果源索引、目的索引或计数小于零或源索引大于此实例的长度。
- 参数异常:如果源索引+计数大于此实例的长度或者目的地索引+计数大于目的地的长度。
例 1:
// C# program to illustrate the
// CopyTo () StringBuilder Method
using System;
using System.Text;
class Geeks {
// Main Method
public static void Main()
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("GeeksForGeeks");
char[] dest = new char[15];
// str index 5 to 5+3 has to
// copy into Copystring
// 3 is no. of character
// 0 is start index of Copystring
str.CopyTo(5, dest, 0, 3);
// Displaying String
Console.Write("The Copied String in "+
"dest Variable is: ");
Console.WriteLine(dest);
}
}
Output:
The Copied String in dest Variable is: For
例 2:
// C# program to illustrate the
// CopyTo() StringBuilder Method
using System;
using System.Text;
class Geeks {
// Main Method
public static void Main()
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str2
= new StringBuilder("GeeksForGeeks");
char[] dest = { 'H', 'e', 'l', 'l', 'o', ' ',
'W', 'o', 'r', 'l', 'd' };
// str index 8 to 8 + 5 has
// to copy into Copystring
// 5 is no of character
// 6 is start index of Copystring
str2.CopyTo(8, dest, 6, 5);
// Displaying the result
Console.Write("String Copied in dest is: ");
Console.WriteLine(dest);
}
}
Output:
String Copied in dest is: Hello Geeks
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处