C# | How to copy a String into another String
String.Copy(String) Method is used to create a new instance of String with the same value as a specified String. In other words, this method is used to copy the data of one string into a new string.
The new string contains same data like an original string but represents a different object reference.
Syntax:
public static string Copy (string value);
Parameter: Here value is the string which is to be copied.
Return Value: The return type of this method is System.String. This method returns a new string that contains the data similar to the value.
Exception: This method will give ArgumentNullException if the value is null.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# program to illustrate Copy() Method using System; class GFG { // Main Method static public void Main() { // string which is to be copied string strA = "GeeksforGeeks"; // Copy the data of strA string // into strB string // using Copy() method string strB = String.Copy(strA); Console.WriteLine("Original String strA: {0}", strA); Console.WriteLine("Copied String strB: {0}", strB); } } |
Original String strA: GeeksforGeeks Copied String strB: GeeksforGeeks
Example 2:
// C# program to check reference of strings using System; class GFG { // Main Method static public void Main() { // string string strA = "GeeksforGeeks"; string strB = "C# Tutorials"; Console.WriteLine("Original string A: {0}", strA); Console.WriteLine("Original string B: {0}", strB); Console.WriteLine(); Console.WriteLine("After copy:"); // copy the data of strA string // into strB string // using Copy() method strB = String.Copy(strA); Console.WriteLine("Value of string A: {0}", strA); Console.WriteLine("Value of string B: {0}", strB); // Checking the reference of both string Console.WriteLine("Is reference of strings is equal : {0}", Object.ReferenceEquals(strA, strB)); } } |
Original string A: GeeksforGeeks Original string B: C# Tutorials After copy: Value of string A: GeeksforGeeks Value of string B: GeeksforGeeks Is reference of strings is equal : False
Reference:
Recommended Posts:
- C# | Copy the elements of a string array to the end of the StringCollection
- C# | Equals(String, String) Method
- Shallow Copy and Deep Copy in C#
- C# | String
- C# | Add a string to the end of the StringCollection
- C# | String.Contains() Method
- C# | String Properties
- C# | String class
- C# | String vs StringBuilder
- C# | String Operators
- C# | How to get the HashCode for the string
- C# | Check if two String objects have the same value | Set-1
- C# | String.IndexOf( ) Method | Set - 1
- C# | Uri.CheckSchemeName(String) Method
- C# | Uri.CheckHostName(String) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



