我想要做的是采取如下的字符串
This is my string and *this text* should be wrapped with <strong></strong>
结果应为
这是我的字符串,此文字应包含
答案 0 :(得分:0)
这个怎么样:
string s = "This is my string and *this text* should be wrapped with <strong></strong>";
int i = 0;
while (s.IndexOf('*') > -1)
{
string tag = i % 0 == 0 ? "<strong>" : "</strong>";
s = s.Substring(0, s.indexOf('*')) + tag + s.Substring(s.indexOf('*')+1);
++i;
}
或Marty Wallace在问题评论中的正则表达式想法,\*[^*]+?\*
答案 1 :(得分:0)
这看起来效果很好:
var str = "This is my string and *this text* should be wrapped with";
var updatedstr = String.Concat(
Regex.Split(str, @"\*")
.Select((p, i) => i % 2 == 0 ? p :
string.Concat("<strong>", p, "</strong>"))
.ToArray()
);