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 //