System.String.Replace 方法 (Char, Char)

方法描述

返回一个新字符串,其中此实例中出现的所有指定 Unicode 字符都替换为另一个指定的 Unicode 字符。

语法定义(C# System.String.Replace 方法 (Char, Char) 的用法)

public string Replace(
	char oldChar,
	char newChar
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
oldChar System-Char 要被替换的 Unicode 字符。
newChar System-Char 要替换出现的所有 oldChar 的 Unicode 字符。
返回值 System.String 与此实例等效的一个字符串,但其中 oldChar 的所有实例都替换为 newChar。

提示和注释

此方法执行序号(区分大小写,不区分区域性)搜索以查找 oldChar。

注意

此方法不修改当前实例的值。 而是返回一个新字符串,其中 oldChar 的所有匹配项均被替换为 newChar。

System.String.Replace 方法 (Char, Char)例子

下面的示例将一系列数字之间的空白替换为逗号,从而创建以逗号分隔的值列表。

using System;

class stringReplace1 {
    public static void Main() {
        String str = "1 2 3 4 5 6 7 8 9";
        Console.WriteLine("Original string: \"{0}\"", str);
        Console.WriteLine("CSV string:      \"{0}\"", 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"
//

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。