char wp = ' ';
char a = 'a';
Assert.True(char.IsWhiteSpace(wp));
Assert.False(char.IsWhiteSpace(a));
但是,当我实现手动优化删除方法时,我意识到这并不像预期得那么好。一些源代码在微软的参考源代码库的char.cs挖掘找到:
public static bool IsWhiteSpace(char c) {
if (IsLatin1(c)) {
return (IsWhiteSpaceLatin1(c));
}
return CharUnicodeInfo.IsWhiteSpace(c);
}
然后CharUnicodeInfo.IsWhiteSpace成了:
internal static bool IsWhiteSpace(char c)
{
UnicodeCategory uc = GetUnicodeCategory(c);
// In Unicode 3.0, U+2028 is the only character which is under the category "LineSeparator".
// And U+2029 is th eonly character which is under the category "ParagraphSeparator".
switch (uc) {
case (UnicodeCategory.SpaceSeparator):
case (UnicodeCategory.LineSeparator):
case (UnicodeCategory.ParagraphSeparator):
return (true);
}
return (false);
}
// whitespace detection method: very fast, a lot faster than Char.IsWhiteSpace
[MethodImpl(MethodImplOptions.AggressiveInlining)] // if it's not inlined then it will be slow!!!
public static bool isWhiteSpace(char ch) {
// this is surprisingly faster than the equivalent if statement
switch (ch) {
case 'u0009': case 'u000A': case 'u000B': case 'u000C': case 'u000D':
case 'u0020': case 'u0085': case 'u00A0': case 'u1680': case 'u2000':
case 'u2001': case 'u2002': case 'u2003': case 'u2004': case 'u2005':
case 'u2006': case 'u2007': case 'u2008': case 'u2009': case 'u200A':
case 'u2028': case 'u2029': case 'u202F': case 'u205F': case 'u3000':
return true;
default:
return false;
}
}
public static string TrimAllWithSplitAndJoin(string str) {
return string.Concat(str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
}
LINQ
这是优雅地声明式地实现这个过程的方法:
public static string TrimAllWithLinq(string str) {
return new string(str.Where(c => !isWhiteSpace(c)).ToArray());
}
static Regex whitespace = new Regex(@"s+", RegexOptions.Compiled);
public static string TrimAllWithRegex(string str) {
return whitespace.Replace(str, "");
}
public static string TrimAllWithInplaceCharArray(string str) {
var len = str.Length;
var src = str.ToCharArray();
int dstIdx = 0;
for (int i = 0; i < len; i++) {
var ch = src[i];
if (!isWhiteSpace(ch))
src[dstIdx++] = ch;
}
return new string(src, 0, dstIdx);
}
public static string TrimAllWithCharArrayCopy(string str) {
var len = str.Length;
var src = str.ToCharArray();
int srcIdx = 0, dstIdx = 0, count = 0;
for (int i = 0; i < len; i++) {
if (isWhiteSpace(src[i])) {
count = i - srcIdx;
Array.Copy(src, srcIdx, src, dstIdx, count);
srcIdx += count + 1;
dstIdx += count;
len--;
}
}
if (dstIdx < len)
Array.Copy(src, srcIdx, src, dstIdx, len - dstIdx);
return new string(src, 0, len);
}
public static string TrimAllWithLexerLoop(string s) {
int length = s.Length;
var buffer = new StringBuilder(s);
var dstIdx = 0;
for (int index = 0; index < s.Length; index++) {
char ch = s[index];
switch (ch) {
case 'u0020': case 'u00A0': case 'u1680': case 'u2000': case 'u2001':
case 'u2002': case 'u2003': case 'u2004': case 'u2005': case 'u2006':
case 'u2007': case 'u2008': case 'u2009': case 'u200A': case 'u202F':
case 'u205F': case 'u3000': case 'u2028': case 'u2029': case 'u0009':
case 'u000A': case 'u000B': case 'u000C': case 'u000D': case 'u0085':
length--;
continue;
default:
break;
}
buffer[dstIdx++] = ch;
}
buffer.Length = length;
return buffer.ToString();;
}
public static string TrimAllWithLexerLoopCharIsWhitespce(string s) {
int length = s.Length;
var buffer = new StringBuilder(s);
var dstIdx = 0;
for (int index = 0; index < s.Length; index++) {
char currentchar = s[index];
if (isWhiteSpace(currentchar))
length--;
else
buffer[dstIdx++] = currentchar;
}
buffer.Length = length;
return buffer.ToString();;
}
public static unsafe string TrimAllWithStringInplace(string str) {
fixed (char* pfixed = str) {
char* dst = pfixed;
for (char* p = pfixed; *p != 0; p++)
if (!isWhiteSpace(*p))
*dst++ = *p;
/*// reset the string size
* ONLY IT DIDN'T WORK! A GARBAGE COLLECTION ACCESS VIOLATION OCCURRED AFTER USING IT
* SO I HAD TO RESORT TO RETURN A NEW STRING INSTEAD, WITH ONLY THE PERTINENT BYTES
* IT WOULD BE A LOT FASTER IF IT DID WORK THOUGH...
Int32 len = (Int32)(dst - pfixed);
Int32* pi = (Int32*)pfixed;
pi[-1] = len;
pfixed[len] = ' ';*/
return new string(pfixed, 0, (int)(dst - pfixed));
}
}
public static unsafe string TrimAllWithStringInplaceV2(string str) {
var len = str.Length;
fixed (char* pStr = str) {
int dstIdx = 0;
for (int i = 0; i < len; i++)
if (!isWhiteSpace(pStr[i]))
pStr[dstIdx++] = pStr[i];
// since the unsafe string length reset didn't work we need to resort to this slower compromise
return new string(pStr, 0, dstIdx);
}
}
String.Replace(“”,“”)
FCIMPL3(Object*, COMString::ReplaceString, StringObject* thisRefUNSAFE, StringObject* oldValueUNSAFE, StringObject* newValueUNSAFE)
public static string TrimAllWithStringReplace(string str) {
// This method is NOT functionaly equivalent to the others as it will only trim "spaces"
// Whitespace comprises lots of other characters
return str.Replace(" ", "");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有