使用 C# Regex 删除空格
假设我们要从以下字符串 str1 中删除空格。
string str1 = "Brad Pitt";
现在,使用 Regex Replace 将空格替换为空。此处,我们使用了 System.Text.RegularExpression。
string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");
让我们来看一个完整的示例。
示例
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
static void Main(string[] args) {
string str1 = "Brad Pitt";
Console.WriteLine(str1);
string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");
Console.WriteLine(str2);
}
}
}输出
Brad Pitt BradPitt
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP