我有这个字符串:ABCDEFGHIJ
我需要使用字符串ZX
替换从第4个位置到第5个位置的字符串。
替换后的结果应该是:ABCZXFGHIJ
但不要使用 string.replace("DE","ZX")
这种方式,我需要使用位置来实现。
我该怎么做呢?
我有这个字符串:ABCDEFGHIJ
我需要使用字符串ZX
替换从第4个位置到第5个位置的字符串。
替换后的结果应该是:ABCZXFGHIJ
但不要使用 string.replace("DE","ZX")
这种方式,我需要使用位置来实现。
我该怎么做呢?
string s = "ABCDEFGH";
s= s.Remove(3, 2).Insert(3, "ZX");
在字符串中添加和删除范围的最简单方法是使用StringBuilder
。
var theString = "ABCDEFGHIJ";
var aStringBuilder = new StringBuilder(theString);
aStringBuilder.Remove(3, 2);
aStringBuilder.Insert(3, "ZX");
theString = aStringBuilder.ToString();
另一种选择是使用String.Substring
,但我认为StringBuilder
代码更易读。
这是一个扩展方法,它不使用StringBuilder或Substring。此方法还允许替换字符串超出源字符串的长度。
//// str - the source string
//// index- the start location to replace at (0-based)
//// length - the number of characters to be removed before inserting
//// replace - the string that is replacing characters
public static string ReplaceAt(this string str, int index, int length, string replace)
{
return str.Remove(index, Math.Min(length, str.Length - index))
.Insert(index, replace);
}
当使用此函数时,如果您希望整个替换字符串尽可能地替换多个字符,请将长度设置为替换字符串的长度:
"0123456789".ReplaceAt(7, 5, "Hello") = "0123456Hello"
否则,您可以指定要删除的字符数:"0123456789".ReplaceAt(2, 2, "Hello") = "01Hello456789"
如果您将长度指定为0,则此函数的行为就像插入函数一样。"0123456789".ReplaceAt(4, 0, "Hello") = "0123Hello456789"
我猜这样更有效率,因为StringBuilder类无需初始化,并且使用了更基本的操作。如果我错了,请纠正我。:)
如果你关心性能,那么你要避免的就是分配内存。如果你在 .Net Core 2.1+ 上,可以使用 string.Create
方法:
public static string ReplaceAt(this string str, int index, int length, string replace)
{
return string.Create(str.Length - length + replace.Length, (str, index, length, replace),
(span, state) =>
{
state.str.AsSpan().Slice(0, state.index).CopyTo(span);
state.replace.AsSpan().CopyTo(span.Slice(state.index));
state.str.AsSpan().Slice(state.index + state.length).CopyTo(span.Slice(state.index + state.replace.Length));
});
}
虽然这种方法比其他方法更难理解,但它是唯一一个每次调用仅分配一个对象的方法:新创建的字符串。
使用String.Substring()
(详情请看这里)来截取左部分,然后是你的替换内容,再加上右部分。通过调整索引直到得到正确结果 :)
类似于以下方式:
string replacement=original.Substring(0,start)+
rep+original.Substring(start+rep.Length);
string s = "ABCDEFG";
string t = "st";
s = s.Remove(4, t.Length);
s = s.Insert(4, t);
t
放回去了。 - Pierrepublic static class StringBuilderExtension
{
public static string SubsituteString(this string OriginalStr, int index, int length, string SubsituteStr)
{
return new StringBuilder(OriginalStr).Remove(index, length).Insert(index, SubsituteStr).ToString();
}
}
您可以尝试类似于以下内容:
string str = "ABCDEFGHIJ";
str = str.Substring(0, 2) + "ZX" + str.Substring(5);
Yet another
public static string ReplaceAtPosition(this string self, int position, string newValue)
{
return self.Remove(position, newValue.Length).Insert(position, newValue);
}
正如其他人所提到的,Substring()
函数是有其存在的原因:
static void Main(string[] args)
{
string input = "ABCDEFGHIJ";
string output = input.Overwrite(3, "ZX"); // 4th position has index 3
// ABCZXFGHIJ
}
public static string Overwrite(this string text, int position, string new_text)
{
return text.Substring(0, position) + new_text + text.Substring(position + new_text.Length);
}
我还将其与StringBuilder
解决方案进行了时间比较,结果是900个滴答声对875个滴答声。因此它略微慢一些。