Home »
C# Tutorial
C# String.Copy() Method with Example
In this tutorial, we will learn about the String.Copy() method with its usage, syntax, and examples.
By IncludeHelp Last updated : April 07, 2023
C# String.Copy() Method
The 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(s)
- str: 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