1. 程式人生 > >教你寫出StringUtils工具類

教你寫出StringUtils工具類

程式碼中,我們有時候要經常進行比較,確定一些條件滿不滿足,例如是否是空,是否是null等等。。。。。
下面我就將我的StringUtil類附上,分享給大家。
附上程式碼:

/*
*程式碼很簡單,就是判斷常用的字串是不是滿足一些條件
*
*裡面有判斷字串是不是url,還有構建url的程式碼等等。
*
*還有很多更使用的判斷,自己看程式碼,老司機我就不多說了
*/
public class StringUtil {
        public StringUtil() {
        }

    public static String trim(String source) {
        return
source != null?source.trim():source; } public static boolean isEmpty(String source) { return source == null || source.length() == 0; } public static boolean isNotEmpty(String source) { return !isEmpty(source); } public static boolean isBlank(String source) { return
source == null || source.length() <= 0 || source.trim().length() <= 0; } public static boolean isNotBlank(String source) { return !isBlank(source); } public static boolean isNumeric(String str) { return !isBlank(str) && str.trim().matches("\\d+"); } public
static boolean isBool(String str) { return !isBlank(str) && (str.trim().equals("true") || str.trim().equals("false")); } public static boolean isEquals(String src, String dest) { return src == null && dest == null?true:(src != null && dest != null?src.trim().equals(dest.trim()):false); } public static boolean isNotEquals(String src, String dest) { return !isEquals(src, dest); } public static boolean isRightUrl(String s) { if (isEmptyStr(s)) return false; else { if (s.startsWith("http://")) return true; else return false; } } public static boolean isInteger(String str) { byte begin = 0; if(isBlank(str)) { return false; } else { str = str.trim(); if(str.startsWith("+") || str.startsWith("-")) { if(str.length() == 1) { return false; } begin = 1; } for(int i = begin; i < str.length(); ++i) { if(!Character.isDigit(str.charAt(i))) { return false; } } return true; } } public static String buildUrl(String url, HashMap<String, String> params){ if(params != null){ StringBuffer buffer = new StringBuffer(url); if(buffer.indexOf("?") == -1){ buffer.append("?"); } buffer.append(buildParam(params)); return buffer.toString(); }else { return url; } } private static String buildParam(HashMap<String, String> params){ StringBuffer buffer = new StringBuffer(); for (Map.Entry<String, String> entry : params.entrySet()) { try { buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } buffer.deleteCharAt(buffer.length()-1); return buffer.toString(); } }