替换字符串模式的第一次出现 [英] Replace first occurrence of pattern in a string
本文介绍了替换字符串模式的第一次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net\">How我更换的一审的.NET中?
Possible Duplicate:
How do I replace the first instance of a string in .NET?
让我们说我有字符串:
string s = "Hello world.";
我怎么能替换单词的第一个 0
您好
为假设富
?
在换句话说,我想落得:
In other words I want to end up with:
"HellFoo world."
我知道如何更换所有的O的,但我想,以取代只是第一个
I know how to replace all the o's but I want to replace just the first one
推荐答案
我认为你可以使用的 Regex.Replace 指定的时间,以取代的最大数...
I think you can use the overload of Regex.Replace to specify the maximum number of times to replace...
var regex = new Regex(Regex.Escape("o"));
var newText = regex.Replace("Hello World", "Foo", 1);
这篇关于替换字符串模式的第一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文