A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-copyto-method/ below:

C# String CopyTo() Method - GeeksforGeeks

C# String CopyTo() Method

Last Updated : 11 Jul, 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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4