我想更换一个给定的字符串中第一次出现。
我怎样才能做到这一点.NET中?
我想更换一个给定的字符串中第一次出现。
我怎样才能做到这一点.NET中?
string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
例:
string str = "The brown brown fox jumps over the lazy dog";
str = ReplaceFirst(str, "brown", "quick");
编辑 :作为@itsmatt 提到的 ,也就是Regex.Replace(字符串,字符串,的Int32),它可以这样做,但可能在运行时更加昂贵,因为它是利用一个功能齐全的解析器在我的方法不会找到一个和三个串级联。
EDIT2:如果这是一个共同的任务,你可能想使该方法的扩展方法:
public static class StringExtension
{
public static string ReplaceFirst(this string text, string search, string replace)
{
// ...same as above...
}
}
使用上面的例子,现在有可能这样写:
str = str.ReplaceFirst("brown", "quick");
正如itsmatt说Regex.Replace然而这一个很好的选择,使他的回答更完整,我会在写些代码示例:
using System.Text.RegularExpressions;
...
Regex regex = new Regex("foo");
string result = regex.Replace("foo1 foo2 foo3 foo4", "bar", 1);
// result = "bar1 foo2 foo3 foo4"
第三个参数,在这种情况下设置为1,是要在从字符串的开头输入字符串替换正则表达式模式的出现的次数。
我希望这可以用一个静态完成Regex.Replace超载但不幸的是它似乎你需要一个正则表达式的实例来完成它。
看看Regex.Replace 。
以“首只”考虑,也许:
int index = input.IndexOf("AA");
if (index >= 0) output = input.Substring(0, index) + "XQ" +
input.Substring(index + 2);
?
或者更一般地说:
public static string ReplaceFirstInstance(this string source,
string find, string replace)
{
int index = source.IndexOf(find);
return index < 0 ? source : source.Substring(0, index) + replace +
source.Substring(index + find.Length);
}
然后:
string output = input.ReplaceFirstInstance("AA", "XQ");
using System.Text.RegularExpressions;
RegEx MyRegEx = new RegEx("F");
string result = MyRegex.Replace(InputString, "R", 1);
会发现第一F
在InputString
,取而代之的是R
在C#语法:
int loc = original.IndexOf(oldValue);
if( loc < 0 ) {
return original;
}
return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
C#扩展方法,将做到这一点:
public static class StringExt
{
public static string ReplaceFirstOccurrence(this string s, string oldValue, string newValue)
{
int i = s.IndexOf(oldValue);
return s.Remove(i, oldValue.Length).Insert(i, newValue);
}
}
请享用
而且因为还有VB.NET考虑,我想献上:
Private Function ReplaceFirst(ByVal text As String, ByVal search As String, ByVal replace As String) As String
Dim pos As Integer = text.IndexOf(search)
If pos >= 0 Then
Return text.Substring(0, pos) + replace + text.Substring(pos + search.Length)
End If
Return text
End Function
假定AA
只需要更换,如果它是在一个字符串的开头:
var newString;
if(myString.StartsWith("AA"))
{
newString ="XQ" + myString.Substring(2);
}
如果您需要更换的第一次出现AA
,该字符串是否与它启动与否,去与马克的解决方案。
一项所述的重载Regex.Replace
需要一个int
为“的时间可以发生置换的最大数量”。 很显然,使用Regex.Replace
纯文本替换似乎有点小题大做,但它肯定简洁:
string output = (new Regex("AA")).Replace(input, "XQ", 1);
对于任何不介意参考Microsoft.VisualBasic
,还有就是Replace
方法 :
string result = Microsoft.VisualBasic.Strings.Replace("111", "1", "0", 2, 1); // "101"
Regex.Replace ,特别是RegEx.Replace(字符串,字符串,整数),也可能是你在找什么。 这或String.IndexOf,这将给你的索引,然后你可以剪切并重建你想要的新文本字符串。
本例展示后者(如首先由证明@大卫Humpohl ):
string str = "Hello WorldWorld";
str = ReplaceFirst(str, "World", "StackOverflow ");
...
string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos >= 0)
{
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
return text;
}
这个例子抽象了的子串(但是更慢),但大概是多少快比一个正则表达式:
var parts = contents.ToString().Split(new string[] { "needle" }, 2, StringSplitOptions.None);
return parts[0] + "replacement" + parts[1];
string abc = "AAAAX1";
if(abc.IndexOf("AA") == 0)
{
abc.Remove(0, 2);
abc = "XQ" + abc;
}