java常用工具類(一)
來源:互聯網
上載者:User
一、String工具類
package com.mkyong.common; import java.util.ArrayList; import java.util.List; /** * * String工具類. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY = ""; /** * <p> * The maximum size to which the padding constant(s) can expand. * </p> */ private static final int PAD_LIMIT = 8192; /** * 功能:將半形的符號轉換成全形符號.(即英文字元轉中文字元) * * @author 宋立君 * @param str * 源字串 * @return String * @date 2014年06月24日 */ public static String changeToFull(String str) { String source = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:'\",<.>/?"; String[] decode = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "。", "@", "#", "$", "%", "︿", "&", "*", "(", ")", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "-", "_", "=", "+", "\", "|", "【", "】", ";", ":", "'", "\"", ",", "〈", "。", "〉", "/", "。" }; String result = ""; for (int i = 0; i < str.length(); i++) { int pos = source.indexOf(str.charAt(i)); if (pos != -1) { result += decode[pos]; } else { result += str.charAt(i); } } return result; } /** * 功能:cs串中是否一個都不包含字元數組searchChars中的字元。 * * @author 宋立君 * @param cs * 字串 * @param searchChars * 字元數組 * @return boolean 都不包含返回true,否則返回false。 * @date 2014年06月24日 */ public static boolean containsNone(CharSequence cs, char... searchChars) { if (cs == null || searchChars == null) { return true; } int csLen = cs.length(); int csLast = csLen - 1; int searchLen = searchChars.length; int searchLast = searchLen - 1; for (int i = 0; i < csLen; i++) { char ch = cs.charAt(i); for (int j = 0; j < searchLen; j++) { if (searchChars[j] == ch) { if (Character.isHighSurrogate(ch)) { if (j == searchLast) { // missing low surrogate, fine, like // String.indexOf(String) return false; } if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) { return false; } } else { // ch is in the Basic Multilingual Plane return false; } } } } return true; } /** * <p> * 編碼為Unicode,格式 '\u0020'. * </p> * * @author 宋立君 * * <pre> * CharUtils.unicodeEscaped(' ') = "\u0020" * CharUtils.unicodeEscaped('A') = "\u0041" * </pre> * * @param ch * 源字串 * @return 轉碼後的字串 * @date 2014年06月24日 */ public static String unicodeEscaped(char ch) { if (ch < 0x10) { return "\\u000" + Integer.toHexString(ch); } else if (ch < 0x100) { return "\\u00" + Integer.toHexString(ch); } else if (ch < 0x1000) { return "\\u0" + Integer.toHexString(ch); } return "\\u" + Integer.toHexString(ch); } /** * <p> * 進行tostring操作,如果傳入的是null,返回Null 字元串。 * </p> * * <pre> * ObjectUtils.toString(null) = "" * ObjectUtils.toString("") = "" * ObjectUtils.toString("bat") = "bat" * ObjectUtils.toString(Boolean.TRUE) = "true" * </pre> * * @param obj * 源 * @return String */ public static String toString(Object obj) { return obj == null ? "" : obj.toString(); } /** * <p> * 進行tostring操作,如果傳入的是null,返回指定的預設值。 * </p> * * <pre> * ObjectUtils.toString(null, null) = null * ObjectUtils.toString(null, "null") = "null" * ObjectUtils.toString("", "null") = "" * ObjectUtils.toString("bat", "null") = "bat" * ObjectUtils.toString(Boolean.TRUE, "null") = "true" * </pre> * * @param obj * 源