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 一個與原始字串值相同但代表不同物件參考的物件。 它與指派操作不同,指派操作會將現有字串引用指派給額外的物件變數。
Important
從 .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 or 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 Method。 以下範例使用該 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/'