替换“&lt;”用“<b>”和“&gt;”用“</b>”

时间:2013-01-28 06:02:11

标签: c# string replace

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>";

这段经文并不意味着什么。但我要做的是将所有“<”替换为“<b>”,将所有“>”替换为“</b>”,全部为“\n\n” “with”<br/>“。

我试图使用:

string y = replace(replace(x,"<","<b>"),">","</b>");
这导致了毁灭性的结果。我想你们都可以猜到,发生了什么。现在我正在寻找一种简单易用的替代方案。我希望,我能说清楚。

7 个答案:

答案 0 :(得分:1)

正如所建议的那样,使用正则表达式可以做到最好。

尝试这样的事情:

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>");

正则表达式的作用是匹配所有关注的字符&gt;它们位于&lt;和&gt;并取代它。组名称为“匹配”,但您可以将其重命名。

答案 1 :(得分:0)

试试这段代码,

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>");

答案 2 :(得分:0)

我会这样做。

String y = Replace(Replace(Replace(Replace(x, "<", "{b}"), ">", "{/b}"), "}", ">"), "{", "<");

首先将<更改为{b},然后将>更改为{/b}

最后,将{}转换为<>

答案 3 :(得分:0)

这基本上是伪UBB格式。您使用了大于/小于符号,而不是方括号。

我建议使用正则表达式 这是一个粗略的代码:

var rgEx = new Regex("<(.+?)>");
string convertedString = rgEx.Replace("Hello is it <me> you're looking for?", "<b>$1</b>");

这个问题是它可能会转换你从代码中渲染的一些html代码。我建议使用标准的UBB格式以避免将来出现问题

答案 4 :(得分:0)

因此,如果我理解正确,您可以使用“<”来表示“开始加粗”,使用“>”来表示“结束加粗”。

使用对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();

答案 5 :(得分:0)

问题是您的替换值包含来自正在替换的原始字符的标记。您需要做的是使用不太可能在原始文本中找到的临时令牌。

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);
}

答案 6 :(得分:0)

这样就可以了。

// 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));