Posted at

スペース・空文字除去

More than 1 year has passed since last update.

スペースとか改行コードとかの空文字を除去

    public static class StringsConvert

{
/// <summary>
/// 空文字除去
/// </summary>
/// <param name="chars"></param>
/// <returns></returns>
public static string RemoveSpace(this string chars)
{
var newChars = new char[chars.Length];
int length = 0;
foreach (var c in chars)
{
if (spaceList.IndexOf(c) < 0)
{
newChars[length] = c;
length++;
}
}
return new string(newChars, 0, length);
}
private static List<char> spaceList = new List<char>
{
(char)0x200B,
(char)0x200C,
(char)0x200D,
(char)0xFEFF,
(char)0x2028,
(char)0x2029,
(char)0x0009,
(char)0x000A,
(char)0x000B,
(char)0x000C,
(char)0x000D,
(char)0x0085,
(char)0x0020,
(char)0x00A0,
(char)0x1680,
(char)0x180E,
(char)0x2000,
(char)0x2001,
(char)0x2002,
(char)0x2003,
(char)0x2004,
(char)0x2005,
(char)0x2006,
(char)0x2007,
(char)0x2008,
(char)0x2009,
(char)0x200A,
(char)0x202F,
(char)0x205F,
(char)0x3000,
};
}