C#字符串操作搜索和替换

时间:2020-03-06 14:22:20  来源:igfitidea点击:

我有一个包含标签形式为<< tag>的字符串。我是否可以通过一种简便的方法,以编程方式用特殊的ascii字符替换这些标签的实例?例如将等价于''/ t'的标签替换为"" <tab>"`之类的标签?

解决方案

正则表达式模式应该可以解决问题。

string s = "...<tab>...";
s = s.Replace("<tab>", "\t");

using System.Text.RegularExpressions;

Regex.Replace(s, "TAB", "\t");//s is your string and TAB is a tab.

public static Regex regex = new Regex("< tab >", RegexOptions.CultureInvariant | RegexOptions.Compiled);
public static string regexReplace = "\t";
string result = regex.Replace(InputText,regexReplace);