package com.co.lane.util;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class StringUtil {
public static final boolean debug = true;
/**
* 转移符号定义
*/
public static final String ESCAPE_CHARACTER = "[\\r\\n\\t]+";
/**
* GB2312编码方式,不能采用系统编码,如果系统是UTF-8,那么一个汉字为3个字节,会超过卡存储。
*/
public static final String CHARSET_CODE = "GB2312";
/**
* 卡存储长度一块为16个字节
*/
public static final int BLOCK_SIZE = 16;
/**
* 32位16进制表示(1个字节占两位16进制符)
*/
public static final String ZERO_32 = "00000000000000000000000000000000";
/**
* check the String is empty(null,"")
*
* @param str
* @return OK...NG
*/
public static final boolean isEmpty(final String str) {
return str == null || str.length() == 0;
}
/**
* check the String is number string
*
* @param str
* @return OK...NG
*/
public static final boolean isNumber(String str) {
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
/**
* String to int,if null return 0,else return int
*
* @param d
* @return Integer
*/
public static int toInt(String str) {
if (StringUtil.isEmpty(str)) {
return 0;
}
if(isNumber(str)){
return new Integer(str).intValue();
}else{
throw new RuntimeException("not a number:" + str);
}
}
/**
* String to integer,if null return null,else return integer
*
* @param d
* @return Integer
*/
public static Integer toInteger(String str) {
if (StringUtil.isEmpty(str)) {
return null;
}
if(isNumber(str)){
return new Integer(str);
}else{
throw new RuntimeException("not a number:" + str);
}
}
/**
* String to double,if null return 0.0,else return double
*
* @param strD
* @return double
*/
public static double toDou(String strD) {
if (isEmpty(strD)) {
return 0.0;
}
if(isNumber(strD)){
return Double.valueOf(strD).doubleValue();
}else{
throw new RuntimeException("not number");
}
}
/**
* String to double,if null return 0.0,else return double
*
* @param strD
* @return double
*/
public static Double toDouble(String strD) {
if (isEmpty(strD)) {
return null;
}
if(isNumber(strD)){
return Double.valueOf(strD);
}else{
throw new RuntimeException("not number");
}
}
/**
* Object to String
*
* @param obj
* @return String
*/
public static String toString(Object obj) {
if (obj == null) {
return "";
}
return obj.toString();
}
/**
* 用Date类获取当前时间
* @return
*/
public static String getSytemDate() {
Date now = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd kk:mm:ss");
String time = sdf.format(now);
return time;
}
/**
* 获取文件名,去掉扩展名
*
* @param fileName
* @return 无扩展名文件名
*/
public static String getFileName(String fileName) {
int endIndex = fileName.lastIndexOf(".");
String name = fileName.substring(0, endIndex);
return name;
}
/**
* set,get方法名转换成字段名<br>
* e.g:<br>
* getId=>id,getName=>name,setUML=>UML;<br>
* 如果是setUML,M是大写的。这种就直接返回UML<br>
*
* @param name
* @return
*/
public static String methodToProperty(String name) {
if (name.startsWith("is")) {
name = name.substring(2);
} else if (name.startsWith("get") || name.startsWith("set")) {
name = name.substring(3);
}
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
name = name.substring(0, 1).toLowerCase(Locale.US) + name.substring(1);
}
return name;
}
/**
* 删除转义符\r,\t,\n等
*
* @param value
* @return
*/
public static String escapeCharacter(String value) {
value = value.replaceAll(ESCAPE_CHARACTER, "");
return value;
}
/**
* set,get方法名转换成字段名<br>
* e.g:<br>
* getId=>id,getName=>name,setUML=>UML;<br>
* 如果是setUML,M是大写的。这种就直接返回UML<br>
*
* @param type
* @param value
* @return
*/
public static Object convertDataType(Class<?> type, Object value) {
String name = type.getSimpleName();
// =========JDK1.7=========
// switch(name){
// case "int":
// case "Integer":
// break;
// case "double":
// case "Double":
// break;
// case "BigDecimal":
// break;
// }
// =========JDK1.7 =========
if("int".equals(name)){
return toInt(value.toString().trim());
}else if("Integer".equals(name)){
return toInteger(value.toString().trim());
}else if("double".equals(name)){
return toDou(value.toString().trim());
}else if("Double".equals(name)){
return toDouble(value.toString().trim());
}else if("BigDecimal".equals(name)){
String tmpValue = value.toString().trim();
return isEmpty(tmpValue) ? null : new BigDecimal(tmpValue);
}
return value;
}
/**
* 把对象JSON化成Map对象存储
*
* @param t
* @return
*/
public static <T> Map<String,Object> convertObject2Map(T t){
Map<String,Object> mapOriginalData = new HashMap<String,Object>();
// TODO 和XmlContextUtil一起完成(应用于打印,前提是对象属性值都已经是可以直接打印,没有再次需要format处理等)
return mapOriginalData;
}
/**
* 字符串转换成16进制字符串
*
* @param data
* @return
*/
public static String String2Hex(String data){
String strHex = "";
if(data == null){
return ZERO_32;
}
try {
strHex = StringUtil.byte2Hex(data.getBytes(CHARSET_CODE));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
return strHex;
}
/**
* 16进制字符串转换成字符串
*
* @param strHex
* @return
*/
public static String hex2String(String strHex){
String data = "";
byte[] b = StringUtil.hex2Byte(strHex);
try {
// 过滤掉字节为0,即生成空字符串
for(int i = 0; i< b.length; i++){
if (b[i] == 0){
data = new String(b, 0, i, CHARSET_CODE);
return data;
}
}
data = new String(b, CHARSET_CODE);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
return data;
}
/**
* 字节码转换成16进制字符串
* 如果不足32位,末尾补0
* @param strHex
* @return
*/
public static String byte2Hex(byte[] b){
String strHex = "";
String tmp = "";
for(int i = 0; i< b.length; i++){
tmp = Integer.toHexString(b[i] & 0XFF);
// 不足补0
if(tmp.length() == 1){
tmp = "0" + tmp;
}
strHex += tmp;
}
strHex = (strHex + ZERO_32).substring(0, BLOCK_SIZE * 2);
return strHex;
}
/**
* 16进制字符串转换成字节码
*
* @param strHex
* @return
*/
public static byte[] hex2Byte(String strHex){
int len = strHex.length();
byte[] b = new byte[len / 2];
for(int i = 0; i< b.length; i++){
b[i] = Integer.decode("0x" + strHex.substring(i * 2, i * 2 + 2)).byteValue();
}
return b;
}
/**
* 获取字符串字节长度
*
* @param value
* @return
*/
public static int getByteLength(String value){
int len = 0;
try {
len = value.getBytes(CHARSET_CODE).length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return len;
}
/**
* 按字节长度分割字符串,默认字节是16字节长度。
*
* @param value 要分割的字符串
* @param limit 分配存储块个数,觉得返回容器的大小,如果不足,则空补充。
* @see # splitByBytelength(String value)
*
* @return 存放分割后的List容器,空字符的时候size=0。
*/
public static String[] splitByBytelength(String value, int limit){
String[] listNewLine = new String[limit];
String[] arrTmp = splitByBytelength(value).toArray(new String[0]);
int len = (arrTmp.length > limit) ? limit : arrTmp.length;
for(int i = 0; i < len; i++){
listNewLine[i] = arrTmp[i];
}
return listNewLine;
}
/**
* 按字节长度分割字符串,默认字节是16字节长度。<br>
* String value = "推荐手机随时A随地上B百度咨询热线"<br>
* int length = 16<br>
* ["推荐手机随时A随","地上B百度咨询热","线"]<br>
* 注意点1:由于[A随]刚好占3个字节,所以抛弃[随]。<br>
* 注意点2:这里刚好是32字节。按照length分割应该是2,实际是3.<br>
*
* 第二点要按照实际情况来确定,比如存储卡读取是【1.先拼接再转换】还是【2.先转换再拼接】。<br>
* 1能节省1个字节空间。但是读取时候一定要全部读取,否则可能产生乱码。而且rfCheckWriteHex方法不便于执行验证<br>
* 2比较安全,可以分别读取块内容。<br>
*
* 综上采用2,返回容器用List,不用String[],不确定长度<br>
*
* @param value 要分割的字符串
*
* @return 存放分割后的List容器
*/
public static List<String> splitByBytelength(String value){
int length = BLOCK_SIZE;
List<String> listNewLine = new ArrayList<String>();
// 未超过一存储块的时候
if(value == null){
return listNewLine;
}
if(getByteLength(value) <= length){
listNewLine.add(value);
return listNewLine;
}
// 超过一存储块的时候要分割字符串
// 放入List中的字符串
String strLeft = "";
int len = 0;
// 每一个字符
String tmp = "";
int tmpLen = 0;
for(int i = 0; i < value.length(); i++){
// 一个一个取字符串判断合计长度,超过则放入容器,否则继续拼接
tmp = value.substring(i, i + 1);
tmpLen = getByteLength(tmp);
len += tmpLen;
if(len > BLOCK_SIZE){
listNewLine.add(strLeft);
len = tmpLen;
strLeft = tmp;
}else{
strLeft += tmp;
}
// 最后剩余字符放入容器
if(i == value.length() - 1 && strLeft.length() > 0){
listNewLine.add(strLeft);
}
}
return listNewLine;
}
/**
* trace string
* @param arg1
* @param arg2
*/
public static void trace(Object arg1, Object arg2){
if(debug){
System.out.println("(" + arg1 + "," + arg2 + ")");
}
}
/**
* trace string
* @param arg
*/
public static void trace(Object arg){
if(debug){
System.out.println(arg);
}
}
}