C# Replace() 方法用于获取一个新字符串,其中该字符串中出现的所有指定 Unicode 字符都替换为另一个指定的 Unicode 字符。
Replace() 方法有两种方法。您也可以替换字符串。
public string Replace(Char first, Char second) public string Replace(String firstString, String secondString)
第一个:是char类型的第一个参数。
第二个:是char类型的第二个参数。
它返回一个字符串。
using System; public class StringExample { public static void Main(string[] args) { string s1 = "Hello F#"; string s2 = s1.Replace('F','C'); Console.WriteLine(s2); } }
输出:
Hello C#
using System; public class StringExample { public static void Main(string[] args) { string s1 = "Hello C#, Hello .Net, Hello RocSchool"; string s2 = s1.Replace("Hello","Cheers"); Console.WriteLine(s2); } }
输出:
Cheers C#, Cheers .Net, Cheers RocSchool