供实际开发调用
package com.hzsh.eomc.common.util;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* a数据处理工具类
* @author ex_songlp
*
*/
public class CDataUtil {
/**
* a该方法用于校验字符串是否为数字
* @param str
* @return
*/
public static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}else {
return true;
}
}
/**
* a判断是否为double类型
* @param str
* @return
*/
public static boolean isDouble(String str) {
try {
Double.parseDouble(str);
}catch(Exception e) {
return false;
}
return true;
}
/**
* a该方法用于校验是否为整型
* @param str
* @return
*/
public static boolean isInt(String str) {
try {
Integer.parseInt(str);
}catch(Exception e) {
return false;
}
return true;
}
/**
* a该方法用于校验是否为图片
* @param fileName
* @return
*/
public static boolean isPicture(String fileName) {
String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.PNG|.png|.PSD|.psd|.GIF|.gif|.BMP|.bmp|.TIFF|.tiff|.PCX|.pcx|.TGA|.tga|.EXIF|.exif|.FPX|.fpx|.SVG|.svg)$";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(fileName);
return matcher.find();
}
/**
* a该方法用于校验是否为文档
* @param fileName
* @return
*/
public static boolean isDocument(String fileName) {
String reg = ".+(.txt|.TXT|.csv|.CSV|.json|.JSON|.doc|.DOC|.docx|.DOCX|.xlsx|.XLSX|.xls|.XLS|.sql|.SQL|.vsd|.VSD|.vsdx|.VSDX|.pdf|.PDF)$";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(fileName);
return matcher.find();
}
/**
* a该方法用于格式化double类型数据,保留小数点后几位,例如:###.00保留小数点后两位
* @param num
* @return
*/
public static String decimalFormat(double num, String bit) {
DecimalFormat df2 = new DecimalFormat("###." + bit);
return df2.format(num);
}
}
package com.hzsh.eomc.common.util;
/**
* 将字符串中的数字和文字分开
* @author Noodles
*
*/
public class Splits {
private String s;
public Splits(String s){
this.s = s;
}
public Splits() {
this("unknow");
}
public String[] getStr(){
String[] str_string = s.split("\\d");// \d 为正则表达式表示[0-9]数字
return str_string;
}
public int[] getNum(){
String[] num_string = s.split("\\D"); // \D 为正则表达式表示非数字
String a = "";
for(String m : num_string){
a += m;
}
String[] num = a.split(""); //将分离出的重新保存在新数组num中不要直接用num_string, 因为在正则表达式对字符串进行选择时若前面的几个字符不符合要求但num_string数组中仍会存有其位置 是空格
int[] inte = new int[num.length];
for(int i =0; i < num.length; i++){
inte[i] = Integer.parseInt(num[i]); //将该数组中的数字存入int数组
}
return inte;
}
//打印字符串中的字母
public String returnStr(){
String a = "";
for(String n : getStr()){
a += n;
}
return a;
}
//打印字符串中的数字
public String returnNum(){
String a = "";
for(int n : getNum()){
a += n;
}
return a;
}
}