StringBuilder.CopyTo Method in C#
This method is used to copy the characters from a specified segment of this instance to a specified segment of a destination Char array.
Syntax:
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
Parameters:
- sourceIndex: It is the starting position in this instance where characters will be copied from. The index is zero-based.
- destination: It is the array where characters will be copied.
- destinationIndex: It is the starting position in destination where characters will be copied. The index is zero-based.
- count: It is the number of characters to be copied.
Exceptions:
- ArgumentNullException: If the destination is null.
- ArgumentOutOfRangeException: If the sourceIndex, destinationIndex, or count, is less than zero or the sourceIndex is greater than the length of this instance.
- ArgumentException: If the sourceIndex + count is greater than the length of this instance or the destinationIndex + count is greater than the length of destination.
Example 1:
using System;
using System.Text;
class Geeks {
public static void Main()
{
StringBuilder str
= new StringBuilder( "GeeksForGeeks" );
char [] dest = new char [15];
str.CopyTo(5, dest, 0, 3);
Console.Write( "The Copied String in " +
"dest Variable is: " );
Console.WriteLine(dest);
}
}
|
Output:
The Copied String in dest Variable is: For
Example 2:
using System;
using System.Text;
class Geeks {
public static void Main()
{
StringBuilder str2
= new StringBuilder( "GeeksForGeeks" );
char [] dest = { 'H' , 'e' , 'l' , 'l' , 'o' , ' ' ,
'W' , 'o' , 'r' , 'l' , 'd' };
str2.CopyTo(8, dest, 6, 5);
Console.Write( "String Copied in dest is: " );
Console.WriteLine(dest);
}
}
|
Output:
String Copied in dest is: Hello Geeks
Reference:
Last Updated :
28 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...