Skip to main content
.NET Framework Class Library
String..::.Copy Method

Creates a new instance of String with the same value as a specified String.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Copy ( _
	str As String _
) As String
public static string Copy(
	string str
)
public:
static String^ Copy(
	String^ str
)
static member Copy : 
        str:string -> string 

Parameters

str
Type: System..::.String
The string to copy.

Return Value

Type: System..::.String
A new string with the same value as str.
Exceptions
ExceptionCondition
ArgumentNullException

str is nullNothingnullptra null reference (Nothing in Visual Basic).

Examples

The following example displays two disimilar strings referenced by two variables, creates a copy of the first string, assigns a reference to the new string to the second variable, then displays the two strings referenced by the variables to demonstrate that the strings are now identical.


' Sample for String.Copy()
Imports System

Class Sample

   Public Shared Sub Main()
      Dim str1 As String = "abc"
      Dim str2 As String = "xyz"
      Console.WriteLine("1) str1 = '{0}'", str1)
      Console.WriteLine("2) str2 = '{0}'", str2)
      Console.WriteLine("Copy...")
      str2 = [String].Copy(str1)
      Console.WriteLine("3) str1 = '{0}'", str1)
      Console.WriteLine("4) str2 = '{0}'", str2)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'1) str1 = 'abc'
'2) str2 = 'xyz'
'Copy...
'3) str1 = 'abc'
'4) str2 = 'abc'
'


// Sample for String.Copy()
using System;

class Sample {
    public static void Main() {
    string str1 = "abc";
    string str2 = "xyz";
    Console.WriteLine("1) str1 = '{0}'", str1);
    Console.WriteLine("2) str2 = '{0}'", str2);
    Console.WriteLine("Copy...");
    str2 = String.Copy(str1);
    Console.WriteLine("3) str1 = '{0}'", str1);
    Console.WriteLine("4) str2 = '{0}'", str2);
    }
}
/*
This example produces the following results:
1) str1 = 'abc'
2) str2 = 'xyz'
Copy...
3) str1 = 'abc'
4) str2 = 'abc'
*/


// Sample for String::Copy()
using namespace System;
int main()
{
   String^ str1 = "abc";
   String^ str2 = "xyz";
   Console::WriteLine( "1) str1 = '{0}'", str1 );
   Console::WriteLine( "2) str2 = '{0}'", str2 );
   Console::WriteLine( "Copy..." );
   str2 = String::Copy( str1 );
   Console::WriteLine( "3) str1 = '{0}'", str1 );
   Console::WriteLine( "4) str2 = '{0}'", str2 );
}

/*
This example produces the following results:
1) str1 = 'abc'
2) str2 = 'xyz'
Copy...
3) str1 = 'abc'
4) str2 = 'abc'
*/

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?