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