我也看了半天才看懂,给大家看看.net自己的trimstart方法
public string TrimStart(params char[] trimChars)
{
if ((trimChars == null) || (trimChars.Length == 0))
{
trimChars = WhitespaceChars;
}
return this.TrimHelper(trimChars, 0);
}
--------------
private string TrimHelper(char[] trimChars, int trimType)
{
int num = this.Length - 1;
int startIndex = 0;
if (trimType != 1)
{
startIndex = 0;
while (startIndex < this.Length)
{
int index = 0;
char ch = this[startIndex];
index = 0;
while (index < trimChars.Length)
{
if (trimChars[index] == ch)
{
break;
}
index++;
}
if (index == trimChars.Length)
{
break;
}
startIndex++;
}
}
if (trimType != 0)
{
num = this.Length - 1;
while (num >= startIndex)
{
int num4 = 0;
char ch2 = this[num];
num4 = 0;
while (num4 < trimChars.Length)
{
if (trimChars[num4] == ch2)
{
break;
}
num4++;
}
if (num4 == trimChars.Length)
{
break;
}
num--;
}
}
int length = (num - startIndex) + 1;
if (length == this.Length)
{
return this;
}
if (length == 0)
{
return Empty;
}
return this.InternalSubString(startIndex, length, false);
}
--------------
private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
{
if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)
{
return this;
}
string str = FastAllocateString(length);
fixed (char* chRef = &str.m_firstChar)
{
fixed (char* chRef2 = &this.m_firstChar)
{
wstrcpy(chRef, chRef2 + startIndex, length);
}
}
return str;
}
回复 引用 查看