Regex.Replace 方法:
在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。
Regex.Replace 语法:
public static string Replace(string input,string pattern,string replacement,RegexOptions options);
input 要搜索匹配项的字符串。
pattern 要匹配的正则表达式模式。
replacement 替换字符串。
options 枚举值的一个按位组合,这些枚举值提供匹配选项。
示例:定义一个字符串,去除字符串中所有的元音字符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
public class vowel
{
public string Disemvowel(string str)
{
return Regex.Replace(str, "[aeiou]", "", RegexOptions.IgnoreCase);
//搜索str字符串中的"aeiou"字符,将其替换为空,忽略大小写
}
}
static void Main(string[] args)
{
shay myObj = new vowel();
string str="Never put off the work till tomorrow what you can put off today.";
string removed = myObj.Disemvowel(b); //引用方法
Console.WriteLine(str);
Console.WriteLine(removed);
Console.ReadKey();
}
}
}
输出: