StringTool.java 6.27 KB
package com.bjivt.util;

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

import org.apache.commons.lang.StringUtils;

 
public final class StringTool {

	public static final String splitOne = ",";  
	public static final String splitTwo = ";"; 

	private StringTool() {
	}
 
	public static String[] splitString(String value, String split) {
		String symbol = "([-+" + split + "])";
		String[] values = null;
		if (!"".equals(value)) {
			if ((value.lastIndexOf(split) + 1) == value.length())
				value = value.substring(0, value.length() - 1);
			values = value.split(symbol);
		}
		return values;
	}
 
	public static String stringJoin(String[] values) {
		String value = "";
		if (values != null && !"null".equals(values) && values.length > 0) {
			for (String key : values) {
				if (key != null)
					value = value + ",'" + key.trim() + "'";
			}
			if (value.length() > 0) {
				value = value.substring(1);
			} else {
				value = "''";
			}
		}
		return value;
	}
 
	public static String Html2Text(String inputString) {
		String htmlStr = inputString; 
		String textStr = "";
		java.util.regex.Pattern p_script;
		java.util.regex.Matcher m_script;
		java.util.regex.Pattern p_style;
		java.util.regex.Matcher m_style;
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;

		try {
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; 
			// }
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";  
			// }
			String regEx_html = "<[^>]+>";  

			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); //  

			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); 

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); 

			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}

		return textStr; 
	}
 /**
  * 敏感符号校验:%=<>'
  * @param value
  * @return
  */
	public static boolean stringJuest(String value) {
		if (value != null
				&& (value.indexOf("%") >= 0 || value.indexOf("=") >= 0
						|| value.indexOf("<") >= 0 || value.indexOf(">") >= 0 || value
						.indexOf("'") >= 0)) {
			return true;
		} else {
			return false;
		}
	}
 /**
  * 邮箱格式校验
  * @param email
  * @return
  */
	public static boolean emailFormat(String email) {
		boolean tag = true;
		final String pattern1 = "^([a-z0-9A-Z]+[_-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(email);
		if (!mat.find()) {
			tag = false;
		}
		return tag;
	}

 /**
  * 手机号码格式校验
  * @param phone
  * @return
  */
	public static boolean checkPhone(String phone) {
		Pattern pattern = Pattern.compile("^13\\d{9}||18\\d{9}||15\\d{9}$");
		Matcher matcher = pattern.matcher(phone);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}
	 /**
	  * 全角转半角
	  * @param QJstr
	  * @return
	  */
	public static String QBchange(String QJstr) {
		String outStr = "";
		String Tstr = "";
		byte[] b = null;

		for (int i = 0; i < QJstr.length(); i++) {
			try {
				Tstr = QJstr.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (b[3] == -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else
				outStr = outStr + Tstr;
		}
		return outStr;
	}

	/**
	 * 
	 * 是否存在sql注入字段
	 * */
	public static boolean sql_inj(String str) {
		String inj_str = "'|and|exec|insert|select|delete|(|)|update|count|*|%|chr|mid|master|truncate|char|declare|;| or |-|+|,";
		String inj_stra[] = inj_str.split("\\|");
		for (int i = 0; i < inj_stra.length; i++) {
			if (str.indexOf(inj_stra[i]) >= 0) {
				return true;
			}
		}
		return false;
	}
 
	public static String urlStringTrans(String value) {
		if (value != null && !"".equals(value)) {
			if (value.indexOf("%") >= 0)
				value = value.replaceAll("%", "%25");
			if (value.indexOf("+") >= 0)
				value = value.replaceAll("\\+", "%2B");
			if (value.indexOf("/") >= 0)
				value = value.replaceAll("/", "%2F");
			if (value.indexOf("?") >= 0)
				value = value.replaceAll("\\?", "%3F");
			if (value.indexOf("#") >= 0)
				value = value.replaceAll("#", "%23");
			if (value.indexOf("&") >= 0)
				value = value.replaceAll("&", "%26");
			if (value.indexOf("=") >= 0)
				value = value.replaceAll("=", "%3D");
		}

		return value;
	}
 
	public static boolean canStringToint(String str) {

		if (StringUtils.isEmpty(str)) {
			return false;
		}
		try {
			Integer.parseInt(str);
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
			return false;
		}

		return true;
	}
 
	public static int stringToInt(String numStr) {
		try {
			if (canStringToint(numStr)) {
				return Integer.parseInt(numStr);
			} else {
				return 0;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		}
	}
	public static String setCharsetStr(String chinaStr) throws  Exception{
		chinaStr=new String(chinaStr.getBytes("gbk"),"gb2312");
		return chinaStr;
	}
	
	
	/**
	 * java-html转义字符的转换
	 * @param content
	 * @return
	 */
	 public static String java2Html(String content) {
		 if(content==null) return "";       
		     String html = content;
		     html = html.replace( "'", "&apos;");
		     html = html.replaceAll( "&", "&amp;");
		     html = html.replace( "\"", "&quot;");  //"
		     html = html.replace( "\t", "&nbsp;&nbsp;");// 替换跳格
		     html = html.replace( " ", "&nbsp;");// 替换空格
		     html = html.replace("<", "&lt;");
		     html = html.replaceAll( ">", "&gt;");
		     return html;
	 }
	
	 
	 /**
	  * 随机产生8位的数字字符
	  * @return
	  */
	 public static String random8Num(){
		 String randomString = "";
         for (int i = 0; i < 8; i++){
            int rand = (int) (Math.random() * 100000000);
            randomString = new Integer(rand).toString();
         }
         return randomString; 
	 }
	 
}