2010-06-01 38 views
2

我想从源字符串中替换一些字符串。所以不应该改变源字符串的大小写。我怎么能做到这一点?我使用.NET替换字符串类型的方法如下这样:如何从源字符串中替换一些字符串,不应该更改源字符串的大小写

var source = "Sadegh"; 
var word = "sadegh"; 
var replaced = source.Replace(word, "<strong>" + word + "</strong>"); 

和替换是:萨德克,但我想萨德克

在此先感谢。

+0

你想在标题情况下,字符串? – AlwaysAProgrammer 2010-06-01 21:07:07

+0

完全没有!在两个根据源字符串 – Sadegh 2010-06-02 16:20:12

回答

1

您想使用正则表达式。我在工作,所以我很遗憾不能把写为你的时间,但在regexlib测试仪是非常有帮助的测试正则表达式:-)

http://regexlib.com/RETester.aspx

+3

我倾向于更喜欢RegexHero,http://regexhero.net/tester/,快速交互式正则表达式测试。不需要回传,所以它是更直接的反馈。 – driis 2010-06-01 21:06:04

+0

哇,我没有遇到过regexhero ...非常好:-) – 2010-06-01 21:17:18

+0

是的,这是非常酷的感谢之人 – Sadegh 2010-06-02 16:43:06

0

非常好,我有代码来做我想要的。

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

和输出谷歌谷歌GOOGLE新闻

相关问题