C#String.Replace 方法


String.Replace 方法

返回一个新字符串,其中已将当前字符串中的指定 Unicode 字符或 String 的所有匹配项替换为其他指定的 Unicode 字符或 String

重载此成员。有关此成员的完整信息(包括语法、用法和示例),请单击重载列表中的相应名称。

  名称 说明
公共方法 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
)

参数

oldChar
类型: System.Char
要被替换的 Unicode 字符。
newChar
类型: System.Char
要替换出现的所有 oldChar 的 Unicode 字符。

返回值

类型: System.String
等效于此实例(除了 oldChar 的所有实例都已替换为 newChar 外)的字符串。  如果在当前实例中找不到 oldChar,此方法返回未更改的当前实例。 
 
C#示例
 

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 中)

 

C#语法
public string Replace(
	string oldValue,
	string newValue
)

参数

oldValue
类型: System.String
要被替换的字符串。
newValue
类型: System.String
要替换出现的所有 oldValue 的字符串。

返回值

类型: 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.

C#示例
The following example demonstrates how you can use the Replace method to correct a spelling error.
 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 //

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM