C# String CopyTo() Method (original) (raw)

Last Updated : 18 Mar, 2025

In C#, the **CopyTo() is a method of String Class. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into an array of Unicode characters.

**Example 1: Using the CopyTo() method to copy characters from a string to a character array.

C# `

// C# program to illustrate the // String.CopyTo() Method // Method using System;

class Geeks { public static void Main() { // Creating a string string s = "GeeksForGeeks";

    // Destination char array
    char[] dest = new char[3];

    //Copying 3 characters from the 5th index 
    //of the string to the destination char array.
    s.CopyTo(5, dest, 0, 3);
    
    // Displaying the copied string
    Console.WriteLine($"The Copied String is: {new string(dest)}");
}

}

`

Output

The Copied String is: For

Syntax of String CopyTo() Method

public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)

**Parameters:

**Exceptions:

**Example 2: Using the CopyTo() method to modify an existing character array.

C# `

// C# program to illustrate the // ToCopy() string method using System;

class Geeks {

public static void Main() 
{ 
    string s = "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 
    s.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