String.Replace 方法

定義

回傳一個新字串,將指定 Unicode 字元或 String 當前字串中的所有出現替換為另一個指定的 Unicode 字元或 String

多載

名稱 Description
Replace(Char, Char)

回傳一個新字串,該字串中該實例中指定 Unicode 字元的所有出現都被替換為另一個指定的 Unicode 字元。

Replace(String, String)

回傳一個新字串,將當前實例中指定字串的所有出現都被替換為另一個指定的字串。

Replace(Rune, Rune)
Replace(String, String, StringComparison)

回傳一個新字串,將當前實例中指定字串的所有出現都替換為另一個指定的字串,並使用提供的比較型別。

Replace(String, String, Boolean, CultureInfo)

回傳一個新字串,將當前實例中指定字串的所有出現都替換為另一個指定字串,使用提供的文化與大小寫敏感度。

Replace(Char, Char)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

回傳一個新字串,該字串中該實例中指定 Unicode 字元的所有出現都被替換為另一個指定的 Unicode 字元。

public:
 System::String ^ Replace(char oldChar, char newChar);
public string Replace(char oldChar, char newChar);
member this.Replace : char * char -> string
Public Function Replace (oldChar As Char, newChar As Char) As String

參數

oldChar
Char

Unicode 字元將被替換。

newChar
Char

Unicode 字元取代所有出現的 oldChar

傳回

一個與此實例等價的字串,但所有 的 oldChar 實例都被替換為 newChar。 如果 oldChar 目前實例中找不到 ,該方法會回傳該實例,保持不變。

範例

以下範例透過將逗號替換成一列數字之間的空白,建立一個逗號分隔值清單。

string str = "1 2 3 4 5 6 7 8 9";
Console.WriteLine($"Original string: \"{str}\"");
Console.WriteLine($"CSV string:      \"{str.Replace(' ', ',')}\"");

// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
let str = "1 2 3 4 5 6 7 8 9"
printfn $"Original string: \"{str}\""
printfn $"CSV string:      \"{str.Replace(' ', ',')}\""

// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
Class stringReplace1
   Public Shared Sub Main()
      Dim str As [String] = "1 2 3 4 5 6 7 8 9"
      Console.WriteLine("Original string: ""{0}""", str)
      Console.WriteLine("CSV string:      ""{0}""", str.Replace(" "c, ","c))
   End Sub
End Class
' This example produces the following output:
' Original string: "1 2 3 4 5 6 7 8 9"
' CSV string:      "1,2,3,4,5,6,7,8,9"

備註

此方法執行序數(大小寫區分且不影響文化)搜尋以尋找 oldChar

Note

此方法不會修改當前實例的值。 取而代之的是,它會回傳一個新字串,其中所有 的 oldChar 出現都被替換為 newChar

因為這個方法回傳修改過的字串,你可以串接連續呼叫該 Replace 方法,對原始字串進行多次替換。 方法呼叫依序從左到右執行。 下列範例提供一個實例。

string s = new('a', 3);
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
Console.WriteLine($"The final string: '{s}'");

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = new string('a', 3)
printfn $"The initial string: '{s}'"
let s2 = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd')
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As New String("a"c, 3)
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a"c, "b"c).Replace("b"c, "c"c).Replace("c"c, "d"c)
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

另請參閱

適用於

Replace(String, String)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

回傳一個新字串,將當前實例中指定字串的所有出現都被替換為另一個指定的字串。

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue);
public string Replace(string oldValue, string newValue);
public string Replace(string oldValue, string? newValue);
member this.Replace : string * string -> string
Public Function Replace (oldValue As String, newValue As String) As String

參數

oldValue
String

要取代的字串。

newValue
String

用來替換所有出現的 oldValue字串。

傳回

一個等價於當前字串的字串,但所有的 oldValue 實例都被替換為 newValue。 如果 oldValue 目前實例中找不到 ,該方法會回傳該實例,保持不變。

例外狀況

oldValuenull

oldValue 是空字串(“”)。

範例

以下範例說明如何利用此 Replace 方法修正拼字錯誤。

string errString = "This docment uses 3 other docments to docment the docmentation";

Console.WriteLine($"The original string is:{Environment.NewLine}'{errString}'{Environment.NewLine}");

// Correct the spelling of "document".
string correctString = errString.Replace("docment", "document");

Console.WriteLine($"After correcting the string, the result is:{Environment.NewLine}'{correctString}'");

// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//
open System

let errString = "This docment uses 3 other docments to docment the docmentation"

printfn $"The original string is:{Environment.NewLine}'{errString}'{Environment.NewLine}"

// Correct the spelling of "document".

let correctString = errString.Replace("docment", "document")

printfn $"After correcting the string, the result is:{Environment.NewLine}'{correctString}'"

// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//
Public Class ReplaceTest
    
    Public Shared Sub Main()
        Dim errString As String = "This docment uses 3 other docments to docment the docmentation"
                
        Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString)

        ' Correct the spelling of "document".  
        Dim correctString As String = errString.Replace("docment", "document")
      
        Console.WriteLine("After correcting the string, the result is:{0}'{1}'", Environment.NewLine, correctString)
    End Sub
End Class
'
' This code example produces the following output:
'
' The original string is:
' 'This docment uses 3 other docments to docment the docmentation'
'
' After correcting the string, the result is:
' 'This document uses 3 other documents to document the documentation'
'

備註

newValuenull則所有 的 oldValue 出現皆被移除。

Note

此方法不會修改當前實例的值。 取而代之的是,它會回傳一個新字串,其中所有 的 oldValue 出現都被替換為 newValue

此方法執行序數(大小寫區分且不影響文化)搜尋以尋找 oldValue

因為這個方法回傳修改過的字串,你可以串接連續呼叫該 Replace 方法,對原始字串進行多次替換。 方法呼叫依序從左到右執行。 下列範例提供一個實例。

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{s}'");

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

另請參閱

適用於

Replace(Rune, Rune)

來源:
String.Manipulation.cs
public:
 System::String ^ Replace(System::Text::Rune oldRune, System::Text::Rune newRune);
public string Replace(System.Text.Rune oldRune, System.Text.Rune newRune);
member this.Replace : System.Text.Rune * System.Text.Rune -> string
Public Function Replace (oldRune As Rune, newRune As Rune) As String

參數

oldRune
Rune
newRune
Rune

傳回

適用於

Replace(String, String, StringComparison)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

回傳一個新字串,將當前實例中指定字串的所有出現都替換為另一個指定的字串,並使用提供的比較型別。

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue, StringComparison comparisonType);
public string Replace(string oldValue, string? newValue, StringComparison comparisonType);
public string Replace(string oldValue, string newValue, StringComparison comparisonType);
member this.Replace : string * string * StringComparison -> string
Public Function Replace (oldValue As String, newValue As String, comparisonType As StringComparison) As String

參數

oldValue
String

要取代的字串。

newValue
String

用來替換所有出現的 oldValue字串。

comparisonType
StringComparison

決定如何 oldValue 搜尋的列舉值之一在此實例中被搜尋。

傳回

一個等價於當前字串的字串,但所有的 oldValue 實例都被替換為 newValue。 如果 oldValue 目前實例中找不到 ,該方法會回傳該實例,保持不變。

例外狀況

oldValuenull

oldValue 是空字串(“”)。

備註

newValuenull則所有 的 oldValue 出現皆被移除。

Note

此方法不會修改當前實例的值。 取而代之的是,它會回傳一個新字串,其中所有 的 oldValue 出現都被替換為 newValue

此方法透過搜尋,oldValue利用文化與格敏感性 。comparisonType

因為這個方法回傳修改過的字串,你可以串接連續呼叫該 Replace 方法,對原始字串進行多次替換。 方法呼叫依序從左到右執行。 下列範例提供一個實例。

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{s}'");

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

適用於

Replace(String, String, Boolean, CultureInfo)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

回傳一個新字串,將當前實例中指定字串的所有出現都替換為另一個指定字串,使用提供的文化與大小寫敏感度。

public:
 System::String ^ Replace(System::String ^ oldValue, System::String ^ newValue, bool ignoreCase, System::Globalization::CultureInfo ^ culture);
public string Replace(string oldValue, string? newValue, bool ignoreCase, System.Globalization.CultureInfo? culture);
public string Replace(string oldValue, string newValue, bool ignoreCase, System.Globalization.CultureInfo culture);
member this.Replace : string * string * bool * System.Globalization.CultureInfo -> string
Public Function Replace (oldValue As String, newValue As String, ignoreCase As Boolean, culture As CultureInfo) As String

參數

oldValue
String

要取代的字串。

newValue
String

用來替換所有出現的 oldValue字串。

ignoreCase
Boolean

true 比較時忽略外殼; false 否則,

culture
CultureInfo

比較時應該使用的文化。 如果 culturenull,則會使用目前的文化特性。

傳回

一個等價於當前字串的字串,但所有的 oldValue 實例都被替換為 newValue。 如果 oldValue 目前實例中找不到 ,該方法會回傳該實例,保持不變。

例外狀況

oldValuenull

oldValue 是空字串(“”)。

備註

newValuenull則所有 的 oldValue 出現皆被移除。

Note

此方法不會修改當前實例的值。 取而代之的是,它會回傳一個新字串,其中所有 的 oldValue 出現都被替換為 newValue

此方法利用提供的oldValueculture大小寫敏感度進行搜尋以尋找ignoreCase

因為這個方法回傳修改過的字串,你可以串接連續呼叫該 Replace 方法,對原始字串進行多次替換。 方法呼叫依序從左到右執行。 下列範例提供一個實例。

string s = "aaa";
Console.WriteLine($"The initial string: '{s}'");
s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");
Console.WriteLine($"The final string: '{s}'");

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
let s = "aaa"
printfn $"The initial string: '{s}'"
let s2 = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
printfn $"The final string: '{s2}'"

// The example displays the following output:
//       The initial string: 'aaa'
//       The final string: 'ddd'
Module Example
   Public Sub Main()
      Dim s As String = "aaa"
      Console.WriteLine("The initial string: '{0}'", s)
      s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d")
      Console.WriteLine("The final string: '{0}'", s)
   End Sub
End Module
' The example displays the following output:
'       The initial string: 'aaa'
'       The final string: 'ddd'

適用於