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

时间:2022-09-13 07:52:05

I have a string which contain tags in the form < tag >. Is there an easy way for me to programmatically replace instances of these tags with special ascii characters? e.g. replace a tag like "< tab >" with the ascii equivelent of '/t'?

我有一个字符串,其中包含 形式的标签。有没有一种简单的方法让我以编程方式用特殊的ascii字符替换这些标签的实例?例如用'/ t'的ascii equivelent替换像“ ”这样的标签?

4 个解决方案

#1


13  

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

#2


2  

using System.Text.RegularExpressions;

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

#3


2  

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

#4


1  

Regex patterns should do the trick.

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

#1


13  

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

#2


2  

using System.Text.RegularExpressions;

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

#3


2  

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

#4


1  

Regex patterns should do the trick.

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