package com.rc.common;
import java.io.UnsupportedEncodingException;
public class StrHelper {
/**
* 判断输入的字符串参数是否为空
*
* @return boolean 空则返回true,非空则flase
*/
public static boolean isEmpty(String input) {
return null == input || 0 == input.length() || 0 == input.replaceAll("\\s", "").length();
}
/**
* 判断输入的字节数组是否为空
*
* @return boolean 空则返回true,非空则flase
*/
public static boolean isEmpty(byte[] bytes) {
return null == bytes || 0 == bytes.length;
}
/**
* 字节数组转为字符串
*
* @see 该方法默认以ISO-8859-1转码
* @see 若想自己指定字符集,可以使用<code>getString(byte[] data, String charset)</code>方法
*/
public static String getString(byte[] data) {
return getString(data, "ISO-8859-1");
}
/**
* 字节数组转为字符串
*
* @see 如果系统不支持所传入的<code>charset</code>字符集,则按照系统默认字符集进行转换
*/
public static String getString(byte[] data, String charset) {
if (isEmpty(data)) {
return "";
}
if (isEmpty(charset)) {
return new String(data);
}
try {
return new String(data, charset);
} catch (UnsupportedEncodingException e) {
return new String(data);
}
}
/**
* 字符串转为字节数组
* @see 该方法默认以ISO-8859-1转码
* @see 若想自己指定字符集,可以使用<code>getBytes(String str, String charset)</code>方法
*/
public static byte[] getBytes(String data){
return getBytes(data, "ISO-8859-1");
}
/**
* 字符串转为字节数组
* @see 如果系统不支持所传入的<code>charset</code>字符集,则按照系统默认字符集进行转换
*/
public static byte[] getBytes(String data, String charset){
data = (data==null ? "" : data);
if(isEmpty(charset)){
return data.getBytes();
}
try {
return data.getBytes(charset);
} catch (UnsupportedEncodingException e) {
return data.getBytes();
}
}
}