码迷,mamicode.com
首页 > 其他好文 > 详细

判断字符串为空为 null 为 whitespace 工具类

时间:2017-04-06 19:37:53      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:src   bool   int   class   final   技术分享   not   代码   字符   

  •  逻辑梳理

  在项目里经常要对字符串进行判断,这时往往需要在三个方面对其进行判断:

  一,是否为null

    String str = null;

    if (str == null);

  二,是否为空

    String str = "";

    if (str.length() == 0);

  三,是否为 whitespace, 如:“ ”,这就是一个whitespace字符。

    Char char = " ”;

    Character.isWhitespace(cs.charAt(char));


  •   代码示例 
 1     public class StringUtils {
 2         public static boolean isNotBlank(final CharSequence charSequence) {
 3             return !isBlank(charSequence);
 4         }
 5 
 6         public static boolean isBlank(final CharSequence charSequence) {
 7             if (CharSequence == null || (charSequence.length()) == 0) {
 8                 return true;
 9             }
10             for (int i = 0; i < charSequence.length(); i++) {
11                 if (!Character.isWhitespace(charSequence.charAt(i))) {
12                     return false;
13                 }
14             }
15             return true;
16         }
17     }
  • 补充说明:CharSequence 是一个接口,实现它的有下面这些:

技术分享

  给它往里传 String、StringBuffer、StringBuiler。 

判断字符串为空为 null 为 whitespace 工具类

标签:src   bool   int   class   final   技术分享   not   代码   字符   

原文地址:http://www.cnblogs.com/BaiLaowu/p/6674996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!