Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165182
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-06-29 12:16:13


这个StringUtil是比较全面的
包含:
  • 为空判断,   
  • String参数-->map   
  • map --> String参数
  • list --> String
  • String --> list
  • 数组 --> String
  • 根据Unicode编码完美的判断中文汉字和符号
  • 是否全中文
  • 是否全英文
  • 是否中英文夹杂
  • 字符串是否是纯数字
  • 判断是否邮箱地址
  • 获取随机字符串
  • 获取随机数字
  • 两个字符串相等判断

点击(此处)折叠或打开

  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;

  4. /**
  5.  * 字符串工具类
  6.  *
  7.  * @author moxia
  8.  */
  9. public class StringUtil {
  10.     /**
  11.      * 是否为空白字符串或者null
  12.      *
  13.      * @param str
  14.      * @return
  15.      */
  16.     public static boolean isEmpty(String str) {
  17.         return str == null ? true : str.trim().isEmpty();
  18.     }

  19.     /**
  20.      * Map转paramsString
  21.      *
  22.      * @param params
  23.      * @return name=value&name=value
  24.      */
  25.     public static String mapToParamString(Map<String, String> params) {
  26.         String returnString = "";
  27.         for(String key : params.keySet()) {
  28.             returnString += key + "=" + params.get(key) + "&";
  29.         }
  30.         if(!BaseUtil.isEmpty(returnString)) {
  31.             returnString = returnString.substring(0, returnString.length() - 1);
  32.         }
  33.         return returnString;
  34.     }

  35.     /**
  36.      * paramsString转Map
  37.      *
  38.      * @param srcString name=value&name=value
  39.      * @param splitString &
  40.      * @return
  41.      */
  42.     public static Map<String, String> StringToMap(String srcString, String splitString) {
  43.         Map<String, String> map = new HashMap<String, String>();
  44.         String[] srcs = srcString.split(splitString);
  45.         for(String src : srcs) {
  46.             String[] configs = src.split("=");
  47.             if(!BaseUtil.isEmpty(configs) && configs.length > 1) {
  48.                 map.put(configs[0], configs[1]);
  49.             }
  50.         }
  51.         return map;
  52.     }

  53.     /**
  54.      * 把List<String>转换为一个用指定符号分隔的字符串
  55.      */
  56.     public static String listToString(List<String> list, String sign) {
  57.         StringBuilder sb = new StringBuilder();
  58.         if(list != null && list.size() > 0) {
  59.             for(int i = 0; i < list.size(); i++) {
  60.                 if(i < list.size() - 1) {
  61.                     sb.append(list.get(i) + sign);
  62.                 } else {
  63.                     sb.append(list.get(i));
  64.                 }
  65.             }
  66.         }
  67.         return sb.toString();
  68.     }

  69.     /**
  70.      * 指定符号分隔的字符串转List<String>
  71.      *
  72.      * @param srcString
  73.      * @return
  74.      */
  75.     public static List<String> stringToList(String srcString, String sign) {
  76.         List<String> results = new ArrayList<String>();

  77.         if(BaseUtil.isEmpty(srcString)) {
  78.             return results;
  79.         }

  80.         String[] srcStrings = srcString.split(sign);
  81.         for(String src : srcStrings) {
  82.             results.add(src);
  83.         }

  84.         return results;
  85.     }

  86.     /**
  87.      * 数组转字符串
  88.      *
  89.      * @param srcStrings 字符串数组
  90.      * @param sign 分隔符
  91.      * @return
  92.      */
  93.     public static String implode(Set<String> srcStrings, String sign) {
  94.         String returnString = "";
  95.         int i = 1;
  96.         for(String src : srcStrings) {
  97.             if(i == srcStrings.size()) {
  98.                 returnString += src;
  99.             } else {
  100.                 returnString += src + ",";
  101.             }
  102.             i++;
  103.         }
  104.         return returnString;
  105.     }

  106.     public static String get32UUID() {
  107.         String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
  108.         return uuid;
  109.     }

  110.     // 根据Unicode编码完美的判断中文汉字和符号
  111.     private static boolean isChinese(char c) {
  112.         Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
  113.         if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  114.                         || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  115.                         || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  116.                         || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
  117.                         || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  118.                         || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
  119.                         || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
  120.             return true;
  121.         }
  122.         return false;
  123.     }

  124.     /**
  125.      * 是否全中文
  126.      *
  127.      * @param strName
  128.      * @return
  129.      */
  130.     public static boolean isChinese(String strName) {
  131.         char[] ch = strName.toCharArray();
  132.         for(int i = 0; i < ch.length; i++) {
  133.             char c = ch[i];
  134.             if(!isChinese(c)) {
  135.                 return false;
  136.             }
  137.         }
  138.         return true;
  139.     }

  140.     /**
  141.      * 是否全英文
  142.      *
  143.      * @param strName
  144.      * @return
  145.      */
  146.     public static boolean isEnglish(String strName) {
  147.         char[] ch = strName.toCharArray();
  148.         for(int i = 0; i < ch.length; i++) {
  149.             char c = ch[i];
  150.             if(isChinese(c) || Character.isDigit(c)) {
  151.                 return false;
  152.             }
  153.         }
  154.         return true;
  155.     }

  156.     /**
  157.      * 是否中英文夹杂
  158.      *
  159.      * @param strName
  160.      * @return
  161.      */
  162.     public static boolean isChiglish(String strName) {
  163.         boolean hasChi = false;
  164.         boolean hasEng = false;
  165.         char[] ch = strName.toCharArray();
  166.         for(int i = 0; i < ch.length; i++) {
  167.             char c = ch[i];
  168.             if(isChinese(c)) {
  169.                 hasChi = true;
  170.             } else {
  171.                 hasEng = true;
  172.             }
  173.             if(hasChi && hasEng) {
  174.                 return true;
  175.             }
  176.         }
  177.         return false;
  178.     }
  179.     /**
  180.      * 字符串是否是纯数字
  181.      * 10-base
  182.      * @param str
  183.      * @return
  184.      */
  185.     public static boolean isNumeric(String str) {
  186.         char[] ch = str.toCharArray();
  187.         for(int i = 0; i < ch.length; i++) {
  188.             if(!Character.isDigit(ch[i])) {
  189.                 return false;
  190.             }
  191.         }
  192.         return true;
  193.     }

  194.     /**
  195.      * 判断是否邮箱地址
  196.      * @param str
  197.      * @return
  198.      */
  199.     public static boolean isMail(String str){

  200.         String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

  201.         Pattern regex = Pattern.compile(check);
  202.         Matcher matcher = regex.matcher(str);
  203.         boolean isMatched = matcher.matches();

  204.         return isMatched;
  205.     }

  206.     public static String getRandomStr(Integer length) {

  207.         String all = "0123456789ABCDEFGHIJKLMNOPQISTUVWXYZabcdefghijklmnopqistuvwxyz";

  208.         String randomStr = "";
  209.         for (int i = 0; i < length; i++) {
  210.             Integer j = new Random().nextInt(all.length());
  211.             randomStr += all.charAt(j);
  212.         }

  213.         return randomStr;
  214.     }

  215.     public static String getRandomNum(Integer length) {

  216.         String all = "0123456789";

  217.         String randomStr = "";
  218.         for (int i = 0; i < length; i++) {
  219.             Integer j = new Random().nextInt(all.length());
  220.             randomStr += all.charAt(j);
  221.         }

  222.         return randomStr;
  223.     }
  224.     
  225.     
  226.     public static void main(String[] args) {

  227.     }

  228.     public static Boolean equals(String a, String b){
  229.         return (a == b) || (a != null && a.equals(b));
  230.     }
  231. }

阅读(1047) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~