开发者

inject html markup around random words in string using c#

I want to inject html markup around random 开发者_JAVA百科words in string like "this is a string for this question". The result should be like "this is a string <legend title="xyz">for</legend> this question". I want to do it using c#.


You could probably use Regex.Replace:

// Input string
string someMsg = "this is a string for this question";

// Match word boundaries
string pattern = "\bfor\b";
string replacement = "<legend title=\"for\">for</legend>";

// Create the regex object
Regex rgx = new Regex(pattern);
// Replace all instances of <pattern> with <replacement>
string result = rgx.Replace(someMsg, replacement);

Obviously, you could make this more generic to your needs, but the Regex object provides the functionality you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜