c#处理空白字符
空白字符是指在屏幕不会显示出来的字符(如空格,制表符tab,回车换行等)。空格、制表符、换行符、回车、换页垂直制表符和换行符称为 “空白字符”,因为它们为与间距单词和行在打印的页 )的用途可以读取更加轻松。 标记分隔 (一定) 由空白字符和由其他标记,例如运算符和标点。在分析代码时, C 编译器忽略空白字符,除非使用它们作为分隔符或作为字符常数或字符串文本元素。使用空白字符使程序更易于阅读。请注意编译器还将注释作为空白。
下面贴一段c#处理空白字符的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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 ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-04-27 【总结】清除webbrowser cookie/session的6种方法
2017-04-27 ORACLE CASE WHEN 及 SELECT CASE WHEN的用法
2015-04-27 with(nolock)的用法
2015-04-27 SQLServer 语句-创建索引
2015-04-27 数据库SQL优化大总结之 百万级数据库优化方案(转)
2012-04-27 Excel 数据有效性,怎么提示指定的命名区域不存在?
2011-04-27 Asp.Net中几种相似数据绑定标记符号的解释及用法