com.kukababy.utils.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbquery Show documentation
Show all versions of dbquery Show documentation
Unified query of Mongodb and Sql database.
The newest version!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.kukababy.utils;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @describe 描述
* @author [email protected]
* @date 2014-3-26
*/
public class StringUtil {
private static final Logger log = LoggerFactory.getLogger(StringUtil.class);
private static final char zero = '0';
private static final char nine = '9';
private static final char minus = '-';
private static final char point = '.';
private static Pattern IntReg = Pattern.compile("[1-9][0-9]{0,9}");
public static Pattern NumberReg = Pattern.compile("^-?[0-9]{0,20}(\\.[0-9]{1,6})?$");
public static void main(String args[]){
String str = "10000.1.1" ;
isNumber(str);
long s1 = System.currentTimeMillis();
for(int i=0;i<1000;i++){
//boolean flag = isNumber(str);
//if(flag){
//log.info(""+Double.parseDouble(str));
//}
}
for(int i=0;i<10000;i++){
//str2Number(str);
}
log.info(""+Long.parseLong("-0"));
long s2 = System.currentTimeMillis();
log.info(""+(s2-s1));
Object obj = str2Number("111011112");
if(obj instanceof String){
log.info("String = "+obj);
}else if (obj instanceof Integer){
log.info("Integer = "+obj);
}else if (obj instanceof Long){
log.info("Long = "+obj);
}else if (obj instanceof Double){
log.info("Double = "+obj);
}
}
/**
* 判断字符串是否为数值型
*
* @param val
* @return
*/
public static boolean isNumber(String val) {
return NumberReg.matcher(val).matches();
}
/**
* 判断字符串是否为正整数
*
* @param val
* @return
*/
public static boolean isPInt(String val) {
return IntReg.matcher(val).matches();
}
/**
* 把字符串转换成整数、长整数、Double和string 4种类型
*
* @param val
* @return
*/
public static Object str2Number(String val) {
int len = val.length();
if (len == 0||len > 18) {
return val;
}
if (len == 1) {
char firstChar = val.charAt(0);
if (firstChar == zero) {
return 0;
} else if (firstChar == minus) {
return "-";
} else if (firstChar == point) {
return ".";
}
}
boolean hasPoint = false;
char mChar[] = val.toCharArray() ;
for (int i = 0; i < mChar.length; i++) {
if (mChar[i] == minus) {// 如果是减号
if (i == 0) {// 第一次出现减号,继续判断
continue;
} else {
return val;// 字符串
}
}
if (mChar[i] == point) {
if (hasPoint) {// 出现2次
return val;// 字符串
} else {
hasPoint = true;
continue;
}
}
if (zero <= mChar[i] && mChar[i] <= nine) {
continue;
} else {
return val;// 字符串
}
}
if (hasPoint) {// 有小数点
return Double.parseDouble(val);// 有小数点,double类型
} else {
if(mChar[0]==zero){
return val;// 字符串 01
}
if(mChar[0]==minus&&mChar[1]==zero){
return val;// 字符串 -01
}
if (len < 10) {
return Integer.parseInt(val);// 整数
} else {
return Long.parseLong(val);// 长整数
}
}
}
/**
*
* 描述:
*
*
* 数值转换固定位数的字符串
*
*
* @param len
* @param value
* @return
*/
public static String toFixDigit(int len, int value) {
String v = Integer.toString(value);
int i = len - v.length();
StringBuffer buf = new StringBuffer("");
while (i-- > 0) {
buf.append(0);
}
buf.append(value);
return buf.toString();
}
}