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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/94342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:38:15  来源:igfitidea点击:

C# string manipulation search and replace

提问by TK.

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'?

我有一个字符串,其中包含表单中的标签< tag >。有没有一种简单的方法可以用特殊的 ascii 字符以编程方式替换这些标签的实例?如更换标签喜欢"< tab >"用ASCII码equivelent '/t'

采纳答案by Ferruccio

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

回答by ddc0660

Regex patterns should do the trick.

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

回答by Burkhard

using System.Text.RegularExpressions;

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

回答by ddc0660

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