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.


public static string Copy (string str); 


  • str: string whose value to be copied

Return Value

string - returns a copy of the given strong.


string str1 = "IncludeHelp";
creating new string using String.Copy() method:
string str2 = String.Copy(str1);

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


str1: IncludeHelp
str2: IncludeHelp

Reference: String.Copy(String) Method

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.