cae*_*say 12 .net c# string performance memory-management
最近我一直在阅读Java子串方法的一些缺陷 - 特别是与内存有关,以及java如何保持对原始字符串的引用.具有讽刺意味的是,我也正在开发一个服务器应用程序,该应用程序在一秒钟内使用C#.Net的子串实现几十次.这让我想到了......
string.Substring
吗?string.Substring
?是否有更快的方法根据开始/结束位置拆分字符串?sna*_*arf 18
查看.NET的String.Substring实现,子字符串不与原始内容共享内存.
private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
{
if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)
{
return this;
}
// Allocate new (separate) string
string str = FastAllocateString(length);
// Copy chars from old string to new string
fixed (char* chRef = &str.m_firstChar)
{
fixed (char* chRef2 = &this.m_firstChar)
{
wstrcpy(chRef, chRef2 + startIndex, length);
}
}
return str;
}
Run Code Online (Sandbox Code Playgroud)