超强工具类系列

package com.util;

import java.io.Reader;
import java.sql.Clob;
import java.util.Random;

/**
 *
 * @ClassName: StringUtils
 * @Description: 字符串工具类
 * @version 1.0
 * @date 2013-7-9 下午10:25:49
 * @author caoxiaoyang
 *
 */

public class StringUtils {

	public StringUtils() {
	}

	public static String substring(String str, int max) {
		String tmp = empty2Null(str);
		if (tmp == null || tmp.length() <= max)
			return str;
		else
			return str.substring(0, max);
	}

	public static String wrapString(String str){
		if(null==str){
			str="";
		}else if(str.trim().length()<=0){
			str="";
		}
		return str;
	}

	public static String randomString(int length) {
		String str = null;
		if (length <= 0)
			return null;
		String charset = "abcdefghijklmnopqrstuvwxyz1234567890!#$@%&*-=+|/ABCDEFGHIJKLMNOQPRSTUVWXYZ";
		Random r = new Random();
		Random r1 = new Random();
		StringBuffer bf = new StringBuffer();
		int ba = Math.abs(r1.nextInt() % length) + 1;
		for (int i = 0; i < ba; i++) {
			int radix = Math.abs(r.nextInt(ba) % charset.length());
			char c = charset.charAt(radix);
			bf.append(c);
		}

		str = bf.toString();
		return str;
	}

	public static String null2Empty(String s) {
		if (s == null)
			s = "";
		return s;
	}

	public static String empty2Null(String s) {
		if (s != null && s.trim().length() == 0)
			s = null;
		return s;
	}

	public static boolean isNumeric(String str) {
		if (str == null)
			return false;
		int sz = str.length();
		for (int i = 0; i < sz; i++)
			if (!Character.isDigit(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++)
			if (!Character.isWhitespace(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isEmpty(String s) {
		return s == null || s.trim().length() == 0;
	}

	public static String toHtml(String s) {
		String html = s;
		if (s == null || s.length() == 0)
			return "&nbsp";
		char symbol[] = { '&', '<', '>', '"', '\r' };
		String obj[] = { "&", "<", ">", """, "<br>" };
		for (int i = 0; i < symbol.length; i++)
			html = html.replaceAll(String.valueOf(symbol[i]), obj[i]);

		return html;
	}

	public static boolean notEmpty(Object o) {
		return o != null && !"".equals(o.toString().trim())
				&& !"null".equalsIgnoreCase(o.toString().trim())
				&& !"undefined".equalsIgnoreCase(o.toString().trim());
	}

	/**
	 * 转换成sql的查询in条件
	 *
	 * @param s
	 *            ex. "1,2,3,4,5,6,"
	 * @return '1','2','3','4','5','6'
	 */
	public static String toQueryStr(String s) {
		if (s.indexOf(",") != -1) {
			String[] tmp = s.split(",");
			StringBuffer str = new StringBuffer();
			for (int i = 0; i < tmp.length; i++) {
				str.append("'").append(tmp[i].trim()).append("',");
			}
			return str.toString().substring(0, str.lastIndexOf(","));
		}
		return "'" + s + "'";
	}

	/**
	 * 大类型转String
	 * @param clob
	 * @return
	 */
	public static String clob2String(Clob clob){
		if (clob == null){
			return null;
		}
		StringBuffer sb = new StringBuffer(65535);//64K
		Reader clobStream = null;//创建一个输入流对象
		try{
			clobStream = clob.getCharacterStream();
			char[] b = new char[60000];//每次获取60K
			int i = 0;
			while((i = clobStream.read(b)) != -1){
				sb.append(b,0,i);
			}
		}
		catch(Exception ex){
			sb = null;
		}
		finally{
			try{
				if (clobStream != null)
					clobStream.close();
			}
			catch (Exception e) {
			}
		}
		if (sb == null)
			return null;
		else
			return sb.toString();
	}


}

  

posted @ 2021-04-27 17:46  Sunray0330  阅读(34)  评论(0编辑  收藏  举报