String.Copy 方法

创建一个与指定的 String 具有相同值的 String 的新实例。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function Copy ( _
    str As String _
) As String
用法
Dim str As String
Dim returnValue As String

returnValue = String.Copy(str)
public static string Copy (
    string str
)
public:
static String^ Copy (
    String^ str
)
public static String Copy (
    String str
)
public static function Copy (
    str : String
) : String

参数

返回值

与 str 具有相同值的新 String

异常

异常类型 条件

ArgumentNullException

str 为 空引用(在 Visual Basic 中为 Nothing)。

示例

下面的代码示例显示了由两个变量引用的两个不同的字符串,创建第一个字符串的副本,将对新字符串的引用赋给第二个变量,然后显示由两个变量引用的两个字符串,以演示两个字符串现在是相同的。

' 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'
*/
// Sample for String.Copy()
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        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);
    } //main
} //Sample
/*
This example produces the following results:
1) str1 = 'abc'
2) str2 = 'xyz'
Copy...
3) str1 = 'abc'
4) str2 = 'abc'
*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

String 类
String 成员
System 命名空间
Empty
DBNull 类