StringUtils类中isEmpty与isBlank的区别
2018-02-24 20:53
267 查看
org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str)。
StringUtils.isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
StringUtils.isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
实例展示自定义判断方法,实现同样的判断逻辑
View Code再附加个人经常用的几个String的操作
1.字符串编码转换
2.文本追加高亮
StringUtils.isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
System.out.println(StringUtils.isEmpty(null)); //true System.out.println(StringUtils.isEmpty("")); //true System.out.println(StringUtils.isEmpty(" ")); //false System.out.println(StringUtils.isEmpty("dd")); //falseStringUtils.isNotEmpty(String str) 等价于 !isEmpty(String str)
StringUtils.isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
System.out.println(StringUtils.isBlank(null)); //true System.out.println(StringUtils.isBlank("")); //true System.out.println(StringUtils.isBlank(" ")); //true System.out.println(StringUtils.isBlank("dd")); //falseStringUtils.isBlank(String str) 等价于 !isBlank(String str)
实例展示自定义判断方法,实现同样的判断逻辑
/** * 判断对象是否为null,不允许空白串 * * @param object 目标对象类型 * @return */ public static boolean isNull(Object object){ if (null == object) { return true; } if ((object instanceof String)){ return "".equals(((String)object).trim()); } return false; } /** * 判断对象是否不为null * * @param object * @return */ public static boolean isNotNull(Object object){ return !isNull(object); }
System.out.println(StringHandler.isNull(null)); //true System.out.println(StringHandler.isNull("")); //true System.out.println(StringHandler.isNull(" ")); //true System.out.println(StringHandler.isNull("dd")); //false通常我们通过HttpServletRequest获取到的参数,需要经过判空处理,转型然后得到我们想要的值,这里可以进行这些操作的简单封装.如下
View Code再附加个人经常用的几个String的操作
1.字符串编码转换
/** * change UTF8 To GB2312 * @param target * @return */ public static final String UTF82GB2312(String target) { try { return new String(target.getBytes("UTF-8"), "gb2312"); } catch (Exception localException) { System.err.println("UTF8 TO GB2312 change error!"); } return null; } /** * change UTF8 To GBK * @param target * @return */ public static final String UTF82GBK(String target) { try { return new String(target.getBytes("UTF-8"), "GBK"); } catch (Exception localException) { System.err.println("UTF8 TO GBK change error!"); } return null; } /** * change UTF8 To ISO8859-1 * @param target * @return */ public static final String UTF82ISO(String target) { try { return new String(target.getBytes("UTF-8"), "ISO8859-1"); } catch (Exception localException) { System.err.println("UTF8 TO ISO8859-1 change error!"); } return null; } /** * change Windows-1252 To UTF-8 * @param target * @return */ public static final String Windows1252UTF8(String target) { try { return new String(target.getBytes("Windows-1252"), "UTF-8"); } catch (Exception localException) { System.err.println("Windows1252 To UTF8 chage error"); } return null; }
2.文本追加高亮
/** * 给串增加颜色标签 * @param color * @param target * @return */ public static String withColor(String color, String target) { return withColor(color, target,true); } /** * 给串增加颜色标签 * @param color * @param target * @param paramBoolean * @return */ public static String withColor(String color, String target, boolean paramBoolean) { if (paramBoolean) return "<font color='".concat(color).concat("'>").concat(target).concat("</font>"); return target; }
System.out.println(StringHandler.withColor("red","文本串", true));运行结果
<font color='red'>文本串</font>
相关文章推荐
- StringUtils类中isEmpty与isBlank的区别
- StringUtils类中isEmpty与isBlank的区别
- StringUtils类中isEmpty与isBlank的区别
- [Java积累]StringUtils类中isEmpty与isBlank的区别
- StringUtils类中isEmpty与isBlank的区别
- StringUtils类中isEmpty与isBlank的区别
- isEmpty与isBlank的区别
- StringUtils中使用isEmpty与isBlank区别(举例说明)
- StringUtils中 isNotEmpty 和isNotBlank的区别
- isnotblank与isnotempty的区别
- StringUtils: isNotEmpty 和 isNotBlank的区别
- (十一)Java工具类StringUtils中isEmpty、isBlank、isNotEmpty、isNotBlank方法源码详解
- (十一)Java工具类StringUtils中isEmpty、isBlank、isNotEmpty、isNotBlank方法源码详解
- StringUtils的isBlank与isEmpty
- 非空判断方法:IsNotEmpty和isNotBlank的区别。
- StringUtils中 isNotEmpty 和isNotBlank的区别
- StringUtils中 isNotEmpty 和isNotBlank的区别
- StringUtils中 isNotEmpty 和isNotBlank的区别 以及StringUtil类的方法
- StringUtils下的isEmpty、isBlank、isNotEmpty、isNotBlank
- StringUtils.isEmpty和StringUtils.isBlank的区别