String.Replace 方法
重載此成員。有關此成員的完整信息(包括語法、用法和示例),請單擊重載列表中的相應名稱。
名稱 | 說明 | |
---|---|---|
![]() |
Replace(Char, Char) | 返回一個新字符串,其中此實例中出現的所有指定 Unicode 字符都替換為另一個指定的 Unicode 字符。 |
![]() |
Replace(String, String) | 返回一個新字符串,其中當前實例中出現的所有指定字符串都替換為另一個指定的字符串。 |
String.Replace 方法 (Char, Char)
返回一個新字符串,其中此實例中出現的所有指定 Unicode 字符都替換為另一個指定的 Unicode 字符。
命名空間: System
程序集: mscorlib(在 mscorlib.dll 中)
public string Replace( char oldChar, char newChar )
返回值
類型: System.String等效於此實例(除了 oldChar 的所有實例都已替換為 newChar 外)的字符串。 如果在當前實例中找不到 oldChar,此方法返回未更改的當前實例。
The following example creates a comma separated value list by substituting commas for the blanks between a series of numbers.
1 using System; 2 3 class stringReplace1 { 4 public static void Main() { 5 String str = "1 2 3 4 5 6 7 8 9"; 6 Console.WriteLine("Original string: \"{0}\"", str); 7 Console.WriteLine("CSV string: \"{0}\"", str.Replace(' ', ',')); 8 } 9 } 10 // 11 // This example produces the following output: 12 // Original string: "1 2 3 4 5 6 7 8 9" 13 // CSV string: "1,2,3,4,5,6,7,8,9" 14 //
String.Replace 方法 (String, String)
返回一個新字符串,其中當前實例中出現的所有指定字符串都替換為另一個指定的字符串。
命名空間: System
程序集: mscorlib(在 mscorlib.dll 中)
public string Replace( string oldValue, string newValue )
返回值
類型: System.String等效於當前字符串(除了 oldValue 的所有實例都已替換為 newValue 外)的字符串。 如果在當前實例中找不到 oldValue,此方法返回未更改的當前實例。
異常 | 條件 |
---|---|
ArgumentNullException | oldValue 為 null。 |
ArgumentException | oldValue 是空字符串 ("")。 |
If newValue is null, all occurrences of oldValue are removed.
![]() |
---|
This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldValue are replaced bynewValue. |
This method performs an ordinal (case-sensitive and culture-insensitive) search to find oldValue.
1 using System; 2 3 public class ReplaceTest { 4 public static void Main() { 5 6 string errString = "This docment uses 3 other docments to docment the docmentation"; 7 8 Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString); 9 10 // Correct the spelling of "document". 11 12 string correctString = errString.Replace("docment", "document"); 13 14 Console.WriteLine("After correcting the string, the result is:{0}'{1}'", 15 Environment.NewLine, correctString); 16 } 17 } 18 // 19 // This code example produces the following output: 20 // 21 // The original string is: 22 // 'This docment uses 3 other docments to docment the docmentation' 23 // 24 // After correcting the string, the result is: 25 // 'This document uses 3 other documents to document the documentation' 26 //