Java数据类型处理判断

供实际开发调用

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;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值