如何在C#中替换字符串中的换行符?
让我们以为,必须从下面的字符串中消除换行符,空格和制表符空间。
消除.jpg
示例
我们可以利用Replace()字符串的扩展方法来做到这一点。
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = testString.Replace("\n\r", "_").Replace("\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
}输出结果
上面代码的输出是
Hello _ beautiful _ world
示例
我们还可以使用Regex来执行相同的操作。Regex在System.Text.RegularExpressions命名空间中可用。
using System;
using System.Text.RegularExpressions;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = Regex.Replace(testString, @"\n\r|\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
}输出结果
上面代码的输出是
Hello _ beautiful _ world
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短