String.Copy(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
This API should not be used to create mutable strings. See https://go.microsoft.com/fwlink/?linkid=2084035 for alternatives.
public:
static System::String ^ Copy(System::String ^ str);
[System.Obsolete("This API should not be used to create mutable strings. See https://go.microsoft.com/fwlink/?linkid=2084035 for alternatives.")]
public static string Copy (string str);
public static string Copy (string str);
[<System.Obsolete("This API should not be used to create mutable strings. See https://go.microsoft.com/fwlink/?linkid=2084035 for alternatives.")>]
static member Copy : string -> string
static member Copy : string -> string
Public Shared Function Copy (str As String) As String
参数
- str
- String
要复制的字符串。
返回
值与 str
相同的新字符串。
- 属性
例外
str
为 null
。
注解
方法 Copy
返回一个 String 对象,该对象具有与原始字符串相同的值,但表示不同的对象引用。 它不同于赋值操作,后者将现有字符串引用分配给其他对象变量。
重要
从 .NET Core 3.0 开始,此方法已过时。 但是,我们不建议在任何 .NET 实现中使用它。 特别是,由于 .NET Core 3.0 中字符串插入的变化,在某些情况下, Copy
该方法不会创建新字符串,而只是返回对现有实习生字符串的引用。
根据要调用 Copy
方法的原因,有多种替代方法:
如果希望其他字符串实例在修改字符串的操作中使用,请使用原始字符串实例。 由于字符串是不可变的,因此字符串操作会创建新的字符串实例,并且原始字符串不受影响。 在这种情况下,不应将新字符串引用分配给原始字符串变量。 下面的示例进行了这方面的演示。
var original = "This is a sentence. This is a second sentence."; var sentence1 = original.Substring(0, original.IndexOf(".") + 1); Console.WriteLine(original); Console.WriteLine(sentence1); // The example displays the following output: // This is a sentence. This is a second sentence. // This is a sentence.
let original = "This is a sentence. This is a second sentence." let sentence1 = original.Substring(0, original.IndexOf "." + 1) printfn $"{original}" printfn $"{sentence1}" // The example displays the following output: // This is a sentence. This is a second sentence. // This is a sentence.
Dim original = "This is a sentence. This is a second sentence." Dim sentence1 = original.Substring(0, original.IndexOf(".") + 1) Console.WriteLine(original) Console.WriteLine(sentence1) ' The example displays the following output: ' This is a sentence. This is a second sentence. ' This is a sentence.
在这种情况下,在调用
Copy
方法之前 Substring 调用 方法以创建新字符串会不必要地创建新的字符串实例。如果要创建内容与原始字符串相同的可变缓冲区,请调用 String.ToCharArray 或 StringBuilder.StringBuilder(String) 构造函数。 例如:
private static void UseMutableBuffer() { var original = "This is a sentence. This is a second sentence."; var chars = original.ToCharArray(); var span = new Span<char>(chars); var slice = span.Slice(span.IndexOf('.'), 3); slice = MergeSentence(slice); Console.WriteLine($"Original string: {original}"); Console.WriteLine($"Modified string: {span.ToString()}"); static Span<char> MergeSentence(Span<char> span) { if (span.Length == 0) return Span<char>.Empty; span[0] = ';'; span[2] = Char.ToLower(span[2]); return span; } } // The example displays the following output: // Original string: This is a sentence. This is a second sentence. // Modified string: This is a sentence; this is a second sentence.
let mergeSentence (span: Span<char>) = if span.Length = 0 then Span<char>.Empty else span[0] <- '\000' span[2] <- Char.ToLower span[2] span let useMutableBuffer () = let original = "This is a sentence. This is a second sentence." let chars = original.ToCharArray() let span = Span chars let slice = span.Slice(span.IndexOf '.', 3) let slice = mergeSentence slice let span = span.ToString() printfn $"Original string: {original}" printfn $"Modified string: {span}" // The example displays the following output: // Original string: This is a sentence. This is a second sentence. // Modified string: This is a sentence this is a second sentence.
Private Sub UseMutableBuffer() Dim original = "This is a sentence. This is a second sentence." Dim sb = new StringBuilder(original) Dim index = original.IndexOf(".") sb(index) = ";" sb(index + 2) = Char.ToLower(sb(index + 2)) Console.WriteLine($"Original string: {original}") Console.WriteLine($"Modified string: {sb.ToString()}") End Sub ' The example displays the following output: ' Original string: This is a sentence. This is a second sentence. ' Modified string: This is a sentence; this is a second sentence.
如果要创建字符串的可变副本,以便可以使用不安全的代码修改字符串内容,请使用 Marshal.StringToHGlobalUni 方法。 以下示例使用 Marshal.StringToHGlobalUni 方法获取指向非托管内存中复制字符串的位置的指针,将字符串中每个字符的 Unicode 码位递增一个,并将生成的字符串复制回托管字符串。
private static void UseUnmanaged() { var original = "This is a single sentence."; var len = original.Length; var ptr = Marshal.StringToHGlobalUni(original); string? result; unsafe { char *ch = (char *) ptr.ToPointer(); while (len-- > 0) { char c = Convert.ToChar(Convert.ToUInt16(*ch) + 1); *ch++ = c; } result = Marshal.PtrToStringUni(ptr); Marshal.FreeHGlobal(ptr); } Console.WriteLine($"Original string: {original}"); Console.WriteLine($"String from interop: '{result}'"); } // The example displays the following output: // Original string: This is a single sentence. // String from interop: 'Uijt!jt!b!tjohmf!tfoufodf/'
#nowarn "9" open FSharp.NativeInterop let useUnmanaged () = let original = "This is a single sentence." let mutable len = original.Length let ptr = Marshal.StringToHGlobalUni original let mutable ch = ptr.ToPointer() |> NativePtr.ofVoidPtr<char> while len > 0 do len <- len - 1 Convert.ToUInt16(NativePtr.read ch) + 1us |> Convert.ToChar |> NativePtr.write (NativePtr.add ch 1) ch <- NativePtr.add ch 1 let result = Marshal.PtrToStringUni ptr Marshal.FreeHGlobal ptr printfn $"Original string: {original}" printfn $"String from interop: '{result}'" // The example displays the following output: // Original string: This is a single sentence. // String from interop: 'Uijt!jt!b!tjohmf!tfoufodf/'