C# 取代<&引用;加上&书信电报;b>&引用;及&燃气轮机&引用;加上</b>&引用;
这导致了一个毁灭性的结果。我想你们都能猜到,发生了什么事。现在,我正在寻找一个简单易行的替代方案。我希望我能说清楚。试试这个代码片段C# 取代<&引用;加上&书信电报;b>&引用;及&燃气轮机&引用;加上</b>&引用;,c#,string,replace,C#,String,Replace,这导致了一个毁灭性的结果。我想你们都能猜到,发生了什么事。现在,我正在寻找一个简单易行的替代方案。我希望我能说清楚。试试这个代码片段 string y = replace(replace(x,"<","<b>"),">","</b>"); string x=“我是一个猎人,我该做什么。是个好孩子……”; 字符串_val=x.Replace(“”); 以下是我的做法 string x = "I am a hunter, what do i do. <Na
string y = replace(replace(x,"<","<b>"),">","</b>");
string x=“我是一个猎人,我该做什么。是个好孩子……”;
字符串_val=x.Replace(“”);
以下是我的做法
string x = "I am a hunter, what do i do. <Name> is a good boy......";
string _val = x.Replace("<", "<b>");
_val = Regex.Replace(_val, "(?<!<b)>", "</b>");
String y=Replace(Replace(Replace)(Replace)(Replace(x,“,“{/b}”),“}”,“>”,“{“,”,”这基本上是一种伪UBB格式。您使用的不是方括号,而是大于/小于符号
我建议使用正则表达式
下面是一个粗略的代码:
String y = Replace(Replace(Replace(Replace(x, "<", "{b}"), ">", "{/b}"), "}", ">"), "{", "<");
var rgEx=new Regex(“”);
string convertedString=rgEx.Replace(“您好,您要找的是什么?”,“$1”);
问题是,它可能会转换您从代码中呈现的一些html代码。我建议使用标准UBB格式以避免将来出现问题。因此,如果我理解正确,您可以使用“
”来表示“粗体结束”
使用对String.Replace
的复合调用显然是行不通的,因为问题在于替换值包含来自被替换的原始字符的标记。您需要做的是使用在原始文本中不太可能找到的临时标记
String input = "I am a hunter, what do i do...";
StringBuilder sb = new StringBuilder();
foreach(Char c in input) {
switch(c) {
case '<':
sb.Append("<b>");
break;
case '>':
sb.Append("</b>");
break;
case '\r':
break; // ignore \r characters.
case '\n':
sb.Append("<br />");
break;
default:
sb.Append( c );
break;
}
}
return sb.ToString();
var replacementList=新列表()
{
Tuple.Create(“,”CLOSINGTOKEN“,”)
};
string x=“我是猎人,我该做什么。我是个好孩子。”;
foreach(replacementList中的变量tokenTriple)
{
x=x.Replace(tokenTriple.Item1,tokenTriple.Item2);
}
foreach(replacementList中的变量tokenTriple)
{
x=x.Replace(tokenTriple.Item2,tokenTriple.Item3);
}
正如建议的那样,使用正则表达式可以最好地实现这一点
试着这样做:
var replacementList = new List<Tuple<string, string, string>>()
{
Tuple.Create("<", "#OPENINGTOKEN#", "<b>"),
Tuple.Create(">", "#CLOSINGTOKEN#", "</b>")
};
string x = "I am a hunter, what do i do. <Name> is a good boy.";
foreach (var tokenTriple in replacementList)
{
x = x.Replace(tokenTriple.Item1, tokenTriple.Item2);
}
foreach (var tokenTriple in replacementList)
{
x = x.Replace(tokenTriple.Item2, tokenTriple.Item3);
}
string x=“我是一名猎人,我该做什么。他是一个好男孩。他的年龄是岁和岁。\n\n他对自己的孩子充满热情,也是一个勤劳的家伙。他是他的妻子,她是一个可爱的女孩,她非常爱她的丈夫。她的年龄是岁和岁”;
变量y=Regex.Replace(x,“.]*)>”,“${match}”);
正则表达式所做的是匹配位于<和>之间的所有字符EXPECT>,并替换它。组名称为“匹配”,但您当然可以重命名它。这样就可以了
string x = "I am a hunter, what do i do. <Name> is a good boy. His age is <Age> and a <occupation> of <Institute>. \n\n He is passionate about his <passion> and also a hardworking fellow. <WifeName> is his wife and she is a sweet girl, She loves her husband a lot. Her age is <WifeAge> and <occupation> of <Institute>";
var y = Regex.Replace(x, "<(?<match>[^>.]*)>", "<b>${match}</b>");
//查找
字符串匹配模式=@“\”;
//将它们包装在和标记之间。
字符串替换模式=@“$1”;
x=Regex.Replace(x,matchpattern,replacementpattern));
您可以尝试使用regex来执行此操作。您可以显示示例输出吗?我会在Javascript中执行类似的操作:x.replace(/\/g),).replace(/谢谢。这很容易理解。对我来说很有效。
var replacementList = new List<Tuple<string, string, string>>()
{
Tuple.Create("<", "#OPENINGTOKEN#", "<b>"),
Tuple.Create(">", "#CLOSINGTOKEN#", "</b>")
};
string x = "I am a hunter, what do i do. <Name> is a good boy.";
foreach (var tokenTriple in replacementList)
{
x = x.Replace(tokenTriple.Item1, tokenTriple.Item2);
}
foreach (var tokenTriple in replacementList)
{
x = x.Replace(tokenTriple.Item2, tokenTriple.Item3);
}
string x = "I am a hunter, what do i do. <Name> is a good boy. His age is <Age> and a <occupation> of <Institute>. \n\n He is passionate about his <passion> and also a hardworking fellow. <WifeName> is his wife and she is a sweet girl, She loves her husband a lot. Her age is <WifeAge> and <occupation> of <Institute>";
var y = Regex.Replace(x, "<(?<match>[^>.]*)>", "<b>${match}</b>");
// find words and whitespace between < and >
String matchpattern = @"\<([\w\s]+)\>";
// wrap them between <b> and </b> tags.
String replacementpattern = @"<b>$1</b>";
x = Regex.Replace(x,matchpattern,replacementpattern));