开发者

how to replace some string(s) from a source string which shouldn't be changed upper/lowercase of source string

开发者 https://www.devze.com 2023-01-01 15:40 出处:网络
i want to replace some string(s) from a source string. so that shouldn\'t be changed upper/lowercase of source string. how i can do this? I\'m using Replace .NET string type method such below:

i want to replace some string(s) from a source string. so that shouldn't be changed upper/lowercase of source string. how i can do this? I'm using Replace .NET string type method such below:

var source = "Sadegh";
var word = "sadegh";
var replaced = so开发者_运维技巧urce.Replace(word, "<strong>" + word + "</strong>");

and replace is: sadegh but i want Sadegh

thanks in advance.


Do this: http://weblogs.asp.net/jgalloway/archive/2004/02/11/71188.aspx

String.Replace is not good for that. Regex replace is okay.


You want to use a regular expression. I'm at work so I unfortunately can't take the time to write it for you, but the tester at regexlib is very helpful in testing regexes :-)

http://regexlib.com/RETester.aspx


very well, i'm have code to doing that i want.

 string title = "Google google GOOGLE News";
 string pat = "google";
 string res = title;
 foreach (Match m in Regex.Matches(title, pat, RegexOptions.IgnoreCase))
     res = Regex.Replace(res, m.ToString(), "<strong>" + m + "</strong>", RegexOptions.ExplicitCapture);

and output is Google google GOOGLE News

0

精彩评论

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