util工具

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**   
 * @Description:   StringUtil工具类
 * @author: mengmei    
 */
public class StringUtil {
	
	/**
	* @Description: 特殊字符转换
	* @param str
	* @return
	* @return String 
	* @throws
	 */
	public static String translate(String str){
		if (str == null) {
			return "";
		}
		return str.replace("\r\n", " ").replace("\r", " ").replace("|", " ");
	}
	
	/**
	 * 
	* @Description: 判断字符串是否不为空
	* @param str
	* @return
	* @return boolean 
	* @throws
	 */
	public static boolean isNotNull(String str){
		return !(str == null || str.trim().length() == 0);
	}
	
	/**
	 * 
	* @Description: 判断字符串是否为空
	* @param str
	* @return
	* @return boolean 
	* @throws
	 */
	public static boolean isNull(String str){
		return str == null || str.trim().length() == 0 ;
	}
	
	/**
	 * 
	* @Description: 清除掉所有特殊字符
	* @param str
	* @return
	* @return String 
	* @throws
	 */
	public static String stringFilter(String str){
		String regx = "[;\\\\\;、,,]";
		Pattern pattern = Pattern.compile(regx);
		Matcher matcher = pattern.matcher(str);
		return matcher.replaceAll("|").trim();
	}
	
	/**
	 * 
	* @Description: 将对象转换为字符串
	* @param obj
	* @return
	* @return String 
	* @throws
	 */
	public static String nvl(Object obj){
		return obj == null ? "" :obj.toString().trim();
	}
	
	/**
	 * 
	* @Description: 判断2个字符串是否相等
	* @param str
	* @param other
	* @return
	* @return boolean 
	* @throws
	 */
	public static boolean isEq(String str, String other){
		if (str == null) {
			return other == null;
		}
		return str.equals(other);
	}
	
	/**
	 * 
	* @Description: 去掉路径,只返回文件名称
	* @param fileName
	* @return
	* @return String 
	* @throws
	 */
	public static String getFileName(String fileName){
		String fileNameRet = "";
		int index = 0;
		if (isNotNull(fileName)) {
			index = fileName.lastIndexOf("/");
			fileNameRet = fileName.substring(index + 1, fileName.length());
		}else {
			fileNameRet = fileName;
		}
		return fileNameRet;
	}
	
	/**
	 * 
	* @Description: 正则表达式校验
	* @param str
	* @param regx
	* @return
	* @return boolean 
	* @throws
	 */
	public static boolean matchRegx(String str, String regx){
		if (str == null || regx == null) {
			return false;
		}
		Pattern pattern = Pattern.compile(regx);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	/**
	 * 
	* @Description: 特殊字符处理
	* @param str
	* @return
	* @return String 
	* @throws
	 */
	public static String replaceCharacter(String str){
		if (isNull(str)) {
			return " ";
		}
		return str.replace('\r', ' ').replace('\n', ' ');
	}
	
	/**
	 * 
	* @Description: 判断字符串是否为数字
	* @param str
	* @return
	* @return boolean 
	* @throws
	 */
	public static boolean checkNumber(String str){
		//1. -?[\\d]+ 可匹配所有正数、负数数字
		//2. [\\d]+ 可匹配所有正数数字
		//3. -?[\\d]+.?[\\d]+ 可匹配所有数字,包括小数
		String regx = "-?[\\d]+.?[\\d]+";
		Pattern pattern = Pattern.compile(regx);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
}

你可能感兴趣的:(util工具类,StringUtil工具)