使用 C# 正则表达式替换字符串中的部分
设置字符串 −
string str = "Bit and Bat";
假设你需要将 B 和 t 之间的所有内容替换为 A 并大写整个字符串。为此,请使用 Replace -
Regex.Replace(str, "B.t", "BAT");
让我们看下完整代码 -
示例
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
static void Main(string[] args) {
string str = "Bit and Bat";
Console.WriteLine(str);
string res = Regex.Replace(str, "B.t", "BAT");
Console.WriteLine(res);
}
}
}输出
Bit and Bat BAT and BAT
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP