How to copy a String into another String in C#

CsharpServer Side ProgrammingProgramming

To copy a String into another String, the code is as follows −

Example

 Live Demo

using System;
public class Demo {
   static public void Main(){
      string str1 = "Kevin";
      string str2 = String.Copy(str1);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String2 = "+str2);
   }
}

Output

This will produce the following output −

String1 = Kevin
String2 = Kevin

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   static public void Main(){
      string str1 = "Maisie";
      string str2 = "Ryan";
      Console.WriteLine("String1 (Before copying) = "+str1);
      Console.WriteLine("String2 (Before copying) = "+str2);
      str2 = String.Copy(str1);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String2 (Updated) = "+str2);
   }
}

Output

This will produce the following output −

String1 (Before copying) = Maisie String2 (Before copying) = Ryan
String1 = Maisie
String2 (Updated) = Maisie
raja
Published on 10-Dec-2019 15:20:02
Advertisements