这是一个常见问题,但我希望这不会被标记为重复项,因为问题的性质不同(请阅读整个不仅是标题)用字符串替换char(0x10)(优化方式)
未察觉的String.Replace
存在我写了下面:
int theIndex = 0;
while ((theIndex = message.IndexOf(separationChar, theIndex)) != -1) //we found the character
{
theIndex++;
if (theIndex < message.Length)//not in the last position
{
message = message.Insert(theIndex, theTime);
}
else
{
// I dont' think this is really neccessary
break;
}
} //while finding characters
正如你可以看到我更换separationChar的出现在一个名为“theTime”字符串的消息字符串。
现在,这适用于小字符串,但我已经给了一个非常巨大的字符串(大约数百字节的顺序 - 顺便说一下String或StringBuilder的限制??),它需要很多时间...
所以我的问题是:
1)它是更有效的,如果我只是做
oldString=separationChar.ToString();
newString=oldString.Insert(theTime);
message= message.Replace(oldString,newString);
2)是否有任何其他的方式,我可以处理非常长的字符串插入字符串(theTime)以非常快的速度查找一些char 高效的方式??
由于已经提到了很多
使用'StringBuilder'它是一个可变类,所以每个操作不必实例化一个新的字符串,只需修改它。 – Danny
你认为这会对速度产生影响吗? – KansaiRobot
如果你处理足够大的字符串,你有性能问题,我希望它可以帮助是的。 – Danny