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 字碼點遞增一個,並將產生的字串複製到 Managed 字串。
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/'