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       *            源 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.