Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between String.Copy() and String.CopyTo() methods in C#?
The String.Copy() and String.CopyTo() methods in C# serve different purposes for copying string data. String.Copy() creates a new string object with the same content, while String.CopyTo() copies characters from a string into a character array.
Syntax
Following is the syntax for String.Copy() method −
public static string Copy(string str)
Following is the syntax for String.CopyTo() method −
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
Parameters
String.Copy() Parameters:
str − The string to copy.
String.CopyTo() Parameters:
sourceIndex − The index of the first character to copy from the string.
destination − The character array to copy characters into.
destinationIndex − The starting index in the destination array.
count − The number of characters to copy.
Using String.Copy()
The String.Copy() method creates a new string object with the same content as the original string −
Example
using System;
class Demo {
static void Main(String[] args) {
string str1 = "Welcome!";
string str2 = "user";
str2 = String.Copy(str1);
Console.WriteLine("Original string: " + str1);
Console.WriteLine("Copied string: " + str2);
Console.WriteLine("Are they equal? " + (str1 == str2));
Console.WriteLine("Are they same reference? " + ReferenceEquals(str1, str2));
}
}
The output of the above code is −
Original string: Welcome! Copied string: Welcome! Are they equal? True Are they same reference? False
Using String.CopyTo()
The String.CopyTo() method copies characters from a string into a character array at a specified position −
Example
using System;
class Demo {
static void Main(String[] args) {
string str = "This is it!";
char[] ch = new char[5];
// Copy 2 characters starting from index 2 of string to index 0 of array
str.CopyTo(2, ch, 0, 2);
Console.WriteLine("Original string: " + str);
Console.WriteLine("Character array: " + new string(ch));
Console.WriteLine("First two chars: " + ch[0] + ch[1]);
}
}
The output of the above code is −
Original string: This is it! Character array: is First two chars: is
Copying Multiple Segments
Example
using System;
class Demo {
static void Main(String[] args) {
string str = "Hello World";
char[] buffer = new char[10];
// Copy "Hello" (first 5 characters)
str.CopyTo(0, buffer, 0, 5);
// Copy "World" (last 5 characters starting from index 6)
str.CopyTo(6, buffer, 5, 5);
Console.WriteLine("Original string: " + str);
Console.WriteLine("Buffer content: " + new string(buffer));
}
}
The output of the above code is −
Original string: Hello World Buffer content: HelloWorld
Comparison
| Feature | String.Copy() | String.CopyTo() |
|---|---|---|
| Return Type | string | void |
| Method Type | Static | Instance |
| Target | New string object | Character array |
| Copies | Entire string | Specified characters |
| Use Case | Creating string duplicates | String to array conversion |
Conclusion
String.Copy() creates a new string object with identical content, while String.CopyTo() extracts specific characters from a string into a character array. Use Copy() for duplicating strings and CopyTo() for selective character extraction into arrays.
