【发布时间】:2017-04-09 04:40:53
【问题描述】:
String 没有ReplaceAt(),我对如何制作一个符合我需要的体面的函数有点犹豫。我想CPU成本很高,但字符串很小,所以没关系
【问题讨论】:
-
你不是说“没有”而不是“有”吗? :)
String 没有ReplaceAt(),我对如何制作一个符合我需要的体面的函数有点犹豫。我想CPU成本很高,但字符串很小,所以没关系
【问题讨论】:
使用StringBuilder:
StringBuilder sb = new StringBuilder(theString);
sb[index] = newChar;
theString = sb.ToString();
【讨论】:
new StringBuilder(theString) {[index] = newChar}.ToString(); 使用对象初始值设定项,您可以使其成为单行代码
最简单的方法是这样的:
public static string ReplaceAt(this string input, int index, char newChar)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
char[] chars = input.ToCharArray();
chars[index] = newChar;
return new string(chars);
}
现在这是一种扩展方法,因此您可以使用:
var foo = "hello".ReplaceAt(2, 'x');
Console.WriteLine(foo); // hexlo
如果能想出某种方法,只需要制作一个单个数据副本而不是这里的两个副本,那就太好了,但我不确定有什么方法可以做到这一点。 有可能这样做:
public static string ReplaceAt(this string input, int index, char newChar)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
StringBuilder builder = new StringBuilder(input);
builder[index] = newChar;
return builder.ToString();
}
...我怀疑这完全取决于您使用的框架版本。
【讨论】:
string s = "ihj";
char[] array = s.ToCharArray();
array[1] = 'p';
s = new string(array);
【讨论】:
字符串是不可变的对象,因此您不能替换字符串中的给定字符。 您可以做的是创建一个替换给定字符的新字符串。
但是如果你要创建一个新字符串,为什么不使用 StringBuilder:
string s = "abc";
StringBuilder sb = new StringBuilder(s);
sb[1] = 'x';
string newS = sb.ToString();
//newS = "axc";
【讨论】:
突然需要做这个任务,发现了这个话题。 所以,这是我的 linq 风格的变体:
public static class Extensions
{
public static string ReplaceAt(this string value, int index, char newchar)
{
if (value.Length <= index)
return value;
else
return string.Concat(value.Select((c, i) => i == index ? newchar : c));
}
}
然后,例如:
string instr = "Replace$dollar";
string outstr = instr.ReplaceAt(7, ' ');
最后我需要使用 .Net Framework 2,所以我使用了 StringBuilder 类变体。
【讨论】:
StringBuilder 或ToCharArray 解决方案?它可能是字符串的少一份副本(与 StringBuilder 相比),但我怀疑内部发生了很多事情。
如果您的项目 (.csproj) 允许不安全代码,这可能是更快的解决方案:
namespace System
{
public static class StringExt
{
public static unsafe void ReplaceAt(this string source, int index, char value)
{
if (source == null)
throw new ArgumentNullException("source");
if (index < 0 || index >= source.Length)
throw new IndexOutOfRangeException("invalid index value");
fixed (char* ptr = source)
{
ptr[index] = value;
}
}
}
}
您可以将它用作String对象的扩展方法。
【讨论】:
string 的语义 - 影响对该对象的任何其他引用,使string 对象是不可变的文档无效/矛盾。对于内置类,这样做是非常可疑的(强烈的代码气味)。需要一个巨大的警告评论。就个人而言,我不会这样做。如果有人需要一个可变字符串,那么创建一个适当的类,子类化或包装一个char[]。
public string ReplaceChar(string sourceString, char newChar, int charIndex)
{
try
{
// if the sourceString exists
if (!String.IsNullOrEmpty(sourceString))
{
// verify the lenght is in range
if (charIndex < sourceString.Length)
{
// Get the oldChar
char oldChar = sourceString[charIndex];
// Replace out the char ***WARNING - THIS CODE IS WRONG - it replaces ALL occurrences of oldChar in string!!!***
sourceString.Replace(oldChar, newChar);
}
}
}
catch (Exception error)
{
// for debugging only
string err = error.ToString();
}
// return value
return sourceString;
}
【讨论】: