0
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”替换为“ <br/>”。

我尝试使用:

string y = replace(replace(x,"<","<b>"),">","</b>");

这导致了我毁灭性的结果。我想你们都可以猜到,发生了什么。现在我正在寻找一种简单易行的替代解决方案。我希望,我能说清楚。

4

7 回答 7

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

正则表达式的作用是匹配位于 < 和 > 之间的所有字符 excpect > 并替换它。组名称为“匹配”,但您当然可以重命名它。

于 2013-01-28T06:13:35.427 回答
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>");
于 2013-01-28T06:05:47.043 回答
0

这是我将如何做到的。

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

<换成{b}>换成{/b}

最后,转换{}<>

于 2013-01-28T06:10:12.277 回答
0

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

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

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

问题在于它可能会转换您从代码呈现的一些 html 代码。我建议使用标准 UBB 格式以避免将来出现问题

于 2013-01-28T06:12:11.420 回答
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();
于 2013-01-28T06:12:15.923 回答
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);
}
于 2013-01-28T06:13:35.180 回答
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));
于 2013-01-28T06:14:15.590 回答