c# 处理空白字符,空白字符是指在屏幕不会显示出来的字符

时间:2022-06-10 07:02:18

空白字符是指在屏幕不会显示出来的字符(如空格,制表符tab,回车换行等)。空格、制表符、换行符、回车、换页垂直制表符和换行符称为 “空白字符”,因为它们为与间距单词和行在打印的页 )的用途可以读取更加轻松。  标记分隔 (一定) 由空白字符和由其他标记,例如运算符和标点。在分析代码时, C 编译器忽略空白字符,除非使用它们作为分隔符或作为字符常数或字符串文本元素。使用空白字符使程序更易于阅读。请注意编译器还将注释作为空白。

更多技术分享请看博客:http://blog.csdn.net/fuyifang

下面贴一段c#处理空白字符的代码:

public static class TypeExtensions
    {
        // The Trim method only trims 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x0085, 0x2028, and 0x2029.
        // This array adds in control characters.
        public static readonly char[] WhiteSpaceChars = new char[] { (char)0x00, (char)0x01, (char)0x02, (char)0x03, (char)0x04, (char)0x05,
            (char)0x06, (char)0x07, (char)0x08, (char)0x09, (char)0x0a, (char)0x0b, (char)0x0c, (char)0x0d, (char)0x0e, (char)0x0f,
            (char)0x10, (char)0x11, (char)0x12, (char)0x13, (char)0x14, (char)0x15, (char)0x16, (char)0x17, (char)0x18, (char)0x19, (char)0x20,
            (char)0x1a, (char)0x1b, (char)0x1c, (char)0x1d, (char)0x1e, (char)0x1f, (char)0x7f, (char)0x85, (char)0x2028, (char)0x2029 };

        /// <summary>
        /// Gets a value that indicates whether or not the collection is empty.
        /// </summary>
        public static bool IsNullOrBlank(this string s)
        {
            if (s == null || s.Trim(WhiteSpaceChars).Length == 0)
            {
                return true;
            }

            return false;
        }

        public static bool NotNullOrBlank(this string s)
        {
            if (s == null || s.Trim(WhiteSpaceChars).Length == 0)
            {
                return false;
            }

            return true;
        }
}
      /// <summary>
        /// 替换制表符
        /// </summary>
        /// <param name="content">待过滤的内容</param>
        /// <returns>返回过滤完成的内容</returns>
        public static string StringToPattern(string content)
        {
            if (content.Trim().Equals(""))
            {
                return string.Empty;
            }
            else
            {
                for (int i = 0; i < WhiteSpaceChars.Length; i++)
                {
                    content = content.Replace(WhiteSpaceChars[i].ToString(), "");
                }
            }

            return content;
        }

public static string StringToPattern(string Str)
{
    if(Str=="")
        return "";

    string[] c={"\\", "^", "$", "{", "[", ".", "(", "*", "+", "?", "!", "#", "|",")"};
    for(int i=0;i<c.Length;i++)
    {
        Str=Str.Replace(c[i],"\\"+c[i]);
    }
    return Str;
}

msdn字符串(C# 编程指南):https://msdn.microsoft.com/zh-cn/library/ms228362.aspx

摘自:http://www.cnblogs.com/shanyou/p/3535215.html

更多技术分享请看博客:http://blog.csdn.net/fuyifang