Home »
C#.Net
String.Copy() method with example in C#
C# String.Copy() method: Here, we are going to learn about the Copy() method of String class with example.
Submitted by IncludeHelp, on March 18, 2019
C# String.Copy() Method
String.Copy() method is used to copy a string to new string object, it returns a new instance of String with the same value as given string.
Syntax:
public static string Copy (string str);
Parameter: string – whose value to be copied
Return value: string - returns a copy of the given strong.
Example:
Input:
string str1 = "IncludeHelp";
creating new string using String.Copy() method:
string str2 = String.Copy(str1);
Output:
str1: IncludeHelp
str2: IncludeHelp
C# Example to copy a string using String.Copy() method
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str1 = "IncludeHelp";
//creating new string using String.Copy() method
string str2 = String.Copy(str1);
//printing strings
Console.WriteLine("str1: " + str1);
Console.WriteLine("str2: " + str2);
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
str1: IncludeHelp
str2: IncludeHelp
Reference: String.Copy(String) Method