java小知识(整理了一些)

判断为空    
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.
1:if(s == null || s.equals(""));
方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法.
2:if(s == null || s.length() <= 0);
方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法
3:if(s == null || s.isEmpty());

判断字符串是否为空
StringUtils.isNotBlank判断为空
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true

StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false

base64加解密
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
 
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
 
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
posted @ 2016-05-03 10:14  郭郭郭  阅读(109)  评论(0编辑  收藏  举报