StringUtils类中isEmpty与isBlank的区别
org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str)。
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")); //false
StringUtils.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")); //false
StringUtils.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获取到的参数,需要经过判空处理,转型然后得到我们想要的值,这里可以进行这些操作的简单封装.如下
/**
* 从<code>HttpServletRequest</code>中获取<code>String</code>类型的值, 不允许传递空串
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/
public static final String getString(HttpServletRequest request,String paramName){
return getString(request, paramName, false);
} /**
* 从<code>HttpServletRequest</code>中获取<code>String</code>类型的值
*
* 如果传递过来的参数为包含空白字符串的字符,认为为有效值, 否则返回null
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/
public static final String getString(HttpServletRequest request,String paramName,boolean isWithSpace) {
String tmp = request.getParameter(paramName);
if(isWithSpace){
//如果允许包含空格,则使用isEmpty判空
if (!StringUtils.isEmpty(tmp)){
return tmp;
}
}else{
if(!StringUtils.isBlank(tmp)){
return tmp;
}
}
return null;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Long</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/
public static final Long getLong(HttpServletRequest request,String paramName) {
return getLong(request, paramName, -1L);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Long</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Long getLong(HttpServletRequest request,String paramName,Long defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Long value = Long.parseLong(tmp);
return value;
} catch (NumberFormatException e) {
return -1L;
}
}
return defaultValue;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Integer</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/ public static final Integer getInt(HttpServletRequest request,String paramName) {
return getInt(request,paramName, -1);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Integer</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Integer getInt(HttpServletRequest request,String paramName, int defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Integer value = Integer.parseInt(tmp);
return value;
} catch (NumberFormatException e) {
return -1;
}
}
return defaultValue;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Short</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/ public static final Short getShort(HttpServletRequest request,String paramName) {
return getShort(request,paramName, (short)-1);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Short</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Short getShort(HttpServletRequest request,String paramName, short defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Short value = Short.parseShort(tmp);
return value;
} catch (NumberFormatException e) {
return (short)-1;
}
}
return defaultValue;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Byte</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/ public static final Byte getByte(HttpServletRequest request,String paramName) {
return getByte(request,paramName, (byte)-1);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Byte</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Byte getByte(HttpServletRequest request,String paramName, Byte defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Byte value = Byte.parseByte(tmp);
return value;
} catch (NumberFormatException e) {
return (byte)-1;
}
}
return defaultValue;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Double</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/
public static final Double getDouble(HttpServletRequest request,String paramName) {
return getDouble(request, paramName,-1D);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Double</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Double getDouble(HttpServletRequest request,String paramName, Double defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Double value = Double.parseDouble(tmp);
return value;
} catch (NumberFormatException e) {
return -1D;
}
}
return defaultValue;
} /**
* 从<code>HttpServletRequest</code>中获取<code>Float</code>类型的值
*
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @return
* 返回需要的值
*/
public static final Float getFloat(HttpServletRequest request,String paramName) {
return getFloat(request, paramName,-1F);
} /**
* 从<code>HttpServletRequest</code>中获取<code>Float</code>类型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 参数名称
* @param defaultValue
* 默认值
* @return
* 返回需要的值
*/
public static final Float getFloat(HttpServletRequest request,String paramName, Float defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Float value = Float.parseFloat(tmp);
return value;
} catch (NumberFormatException e) {
return -1F;
}
}
return defaultValue;
}
再附加个人经常用的几个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>
转载请注明出处:[http://www.cnblogs.com/dennisit/p/3705374.html]
StringUtils类中isEmpty与isBlank的区别的更多相关文章
- StringUtils类中 isEmpty() 与 isBlank()的区别
org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...
- java中StringUtils中isEmpty 和isBlank的区别
StringUtils在commons-lang-2.2.jar包中:org.apache.commons.lang.StringUtils ; StringUtils方法的操作对象是java.lan ...
- StringUtils中isEmpty 和isBlank的区别
StringUtils在commons-lang-2.2.jar包中:org.apache.commons.lang.StringUtils ; StringUtils方法的操作对象是java.lan ...
- StringUtils中 isEmpty 和isBlank的区别
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出Nu ...
- org.apache.commons.lang.StringUtils中isEmpty和isBlank的区别
public static boolean isEmpty(String str) 判断某字符串是否为空,为空的标准是str==null或str.length()==0 StringUtils.isE ...
- StringUtils 中 isEmpty 和 isBlank 的区别
在项目的工作学习中经常用到了 apache commons 中的 StringUtils 的 isBlank 和 isEmpty 来判断字符串是否为空,这个方法都是判断字符串是否为空做判断的,以至于 ...
- StringUtils里的isEmpty和isBlank的区别
这边首先以一个简单的测试代码来解释这两者的区别: @Test void stringTest(){ String a = " "; boolean empty = StringUt ...
- org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别
相信很多java程序员在写代码的时候遇到判断某字符串是否为空的时候会用到StringUtils类中isBlank和isEmpty方法,这两个方法到底有什么区别呢?我们用一段代码来阐述这个区别吧: @T ...
- java判断一个字符串是否为空,isEmpty和isBlank的区别
转载于:https://blog.csdn.net/liusa825983081/article/details/78246792 实际应用中,经常会用到判断字符串是否为空的逻辑 比较简单的就是用 S ...
随机推荐
- 基于OSGI.NET的MVC插件式开发
最近在研究OSGI.NET插件式开发框架.官方网站提供了一个基于OSGI.NET的插件仓库.下载官方的SDK包安装后VS项目模板会多出一组iOpenWorks项目模板.在学习过程中,发现通过iOpen ...
- python全栈开发day42-固定定位等
一.今日内容: 1.绝对定位盒子居中用法 left:50% margin-left:-盒子的一半宽度. 2.固定定位和固定定位的用法 返回顶部 固定导航栏: 3.阿里的字体图 ...
- split应用
/* input:"5 35 53 2 3" output:"2 3 5 35 53" */ public class RegexDemo4 { public ...
- 教程:在 Visual Studio 中开始使用 Flask Web 框架
教程:在 Visual Studio 中开始使用 Flask Web 框架 Flask 是一种轻量级 Web 应用程序 Python 框架,为 URL 路由和页面呈现提供基础知识. Flask 被称为 ...
- 洛谷 P1162 填涂颜色【DFS】
题目链接:https://www.luogu.org/problemnew/show/P1162 题目描述 由数字 0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字 1 构成,围圈时只走上下左右 4 ...
- mvc返回多个结果集,返回多个视图
System.Web.Mvc.ViewPage<dynamic> public ActionResult Index() { IDictionary< ...
- react输入 撤销
销毁阶段可以使用的函数:componentWillUnmount:在删除组件之前进行清理操作,比如计时器和事件监听器.因为这些函数都是开发者手动加上去的,react不知道,必须进行手动清理. 实例第一 ...
- 2016年3月9日Android实习日记
1. 解决 org.eclipse.swt.SWTException: Graphic is disposed 问题. 参考:http://www.xuebuyuan.com/1896964.html ...
- JVM的Client模式与Server模式
概述 JVM有两种运行模式Server与Client.两种模式的区别在于,Client模式启动速度较快,Server模式启动较慢:但是启动进入稳定期长期运行之后Server模式的程序运行速度比Clie ...
- Xshell 连接 CentOS 7 与 Ubuntu Server
操作系统:windows 7 应用软件:Ware Workstation & Xshell 5 Linux:CentOS 7 Minimal & Ubuntu Server 16 == ...