Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Copy Method

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

[Visual Basic]
Public Shared Function Copy( _
   ByVal str As String _
) As String
[C#]
public static string Copy(
 string str
);
[C++]
public: static String* Copy(
 String* str
);
[JScript]
public static function Copy(
   str : String
) : String;

Parameters

str
The String to copy.

Return Value

A new String with the same value as str.

Exceptions

Exception Type Condition
ArgumentNullException str is a null reference (Nothing in Visual Basic).

Example

[Visual Basic] 
' 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'
'

[C#] 
// 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'
*/

[C++] 
// Sample for String::Copy()
#using <mscorlib.dll>

using namespace System;

int main() {
    String* str1 = S"abc";
    String* str2 = S"xyz";
    Console::WriteLine(S"1) str1 = '{0}'", str1);
    Console::WriteLine(S"2) str2 = '{0}'", str2);
    Console::WriteLine(S"Copy...");
    str2 = String::Copy(str1);
    Console::WriteLine(S"3) str1 = '{0}'", str1);
    Console::WriteLine(S"4) str2 = '{0}'", str2);
}
/*
This example produces the following results:
1) str1 = 'abc'
2) str2 = 'xyz'
Copy...
3) str1 = 'abc'
4) str2 = 'abc'
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | Empty | DBNull

Show:
© 2014 Microsoft. All rights reserved.