[Java] Java 常用字符串操作总结 →→→→→进入此内容的聊天室

来自 , 2019-07-20, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/228499b5
  1. 1. String转ASCII码
  2.  
  3. public static String stringToAscii(String value) {
  4.         StringBuffer sbu = new StringBuffer();
  5.         char[] chars = value.toCharArray();
  6.         for (int i = 0; i < chars.length; i++) {
  7.             if (i <= chars.length - 1) {
  8.                 int ii = chars[i];
  9.                 sbu.append(Integer.toHexString(ii));
  10.             } else {
  11.                 sbu.append((int) chars[i]);
  12.             }
  13.         }
  14.         return sbu.toString();
  15.     }
  16. 2. ASCII码转String
  17.  
  18.  
  19. public static String asciiToString(String value) {
  20.         if("".equals(value) || value == null){
  21.             return "";
  22.         }
  23.         StringBuffer sBuffer = new StringBuffer();
  24.         char[] chars = value.toCharArray();
  25.         for (int i = 0; i < chars.length; i = i + 2) {
  26.             if (i < chars.length - 1) {
  27.                 int ii = Integer.valueOf(
  28.                     Character.toString(chars[i]) + Character.toString(chars[i + 1]), 16);
  29.                 sBuffer.append((char) (ii));
  30.             }
  31.         }
  32.         return sBuffer.toString();
  33.     }
  34.  
  35.  
  36. 3. 输入流转字节数组
  37.  
  38.  
  39. public static String inputStream2Byte(InputStream inputStream) throws IOException {
  40.         byte[] buffer = new byte[1024];
  41.         int len = -1;
  42.         while ((len = inputStream.read(buffer)) != -1) {
  43.             bos.write(buffer, 0, len);
  44.         }
  45.         bos.close();
  46.         return new String(bos.toByteArray(), "UTF-8");
  47.     }
  48. 4. 输入流转字符串
  49.  
  50.  
  51. public static String InputStreamTOString(InputStream in) throws Exception {
  52.          
  53.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  54.         byte[] data = new byte[1024];
  55.         int count = -1;
  56.         while ((count = in.read(data, 0, 2048)) != -1)
  57.             outStream.write(data, 0, count);
  58.          
  59.         data = null;
  60.         return new String(outStream.toByteArray());
  61.     }
  62.  
  63.  
  64. 5. 判断输入是不是包含中文
  65.  
  66.  
  67. public static boolean isChinese(char c) {
  68.        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  69.                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
  70.                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
  71.                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
  72.            return true;
  73.        }
  74.        return false;
  75.    }
  76.  
  77.    public static boolean isChinese(String strName) {
  78.        char[] ch = strName.toCharArray();
  79.        for (int i = 0; i < ch.length; i++) {
  80.            char c = ch[i];
  81.            if (isChinese(c)) {
  82.                return true;
  83.            }
  84.        }
  85.        return false;
  86.    }
  87.   
  88.  
  89. 6. 检验子网掩码的合法性:子网掩码转化为二进制之后不能全是1,也不能全是0,必须是连续的1或者连续的0,也就是说10不能交替出现。
  90.  
  91.   实现方式,先将4个整数字段按照每八位拼接起来,软后转化为二进制显示方式,使用正则表达式进行匹配。
  92.  
  93.  
  94. public static boolean checkMask(String maskStr){
  95.       String[] ips = maskStr.split("\\.");
  96.       String binaryVal = "";
  97.       for (int i = 0; i < ips.length; i++)
  98.       {
  99.           String binaryStr = Integer.toBinaryString(Integer.parseInt(ips[i]));  
  100.           Integer times = 8 - binaryStr.length();
  101.              
  102.           for(int j = 0; j < times; j++)
  103.           {
  104.               binaryStr = "0" +  binaryStr;  //补齐八位,每次需要进行八位合并
  105.           }
  106.           binaryVal += binaryStr;
  107.       }
  108.       Pattern regxPattern = Pattern.compile("^[1]*[0]*$");
  109.       if(regxPattern.matcher(binaryVal).matches())
  110.       {
  111.           return true;
  112.       }else {
  113.           return false;
  114.       }
  115.   }
  116.  
  117.  
  118. 7. 检查IP的合法性,并转化为整数表示
  119.  
  120. public static boolean validIP(String ip) {
  121.         if (ip == null) {
  122.             return false;
  123.         }
  124.      String IP_REGEX  = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."
  125.                                                        + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."
  126.                                                        + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\."
  127.                                                        + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
  128.         Pattern     ipPattern           = Pattern.compile(IP_REGEX);
  129.         Matcher matcher = ipPattern.matcher(ip);
  130.         return matcher.matches();
  131.     }
  132.  
  133.     public static Integer ipString2Int(String strIp){
  134.         if(!validIP(strIp)) return null;
  135.  
  136.         int[] ip = new int[4];
  137.         String[] ips = strIp.split("\\.");
  138.  
  139.         ip[0] = Integer.parseInt(ips[0]);
  140.         ip[1] = Integer.parseInt(ips[1]);
  141.         ip[2] = Integer.parseInt(ips[2]);
  142.         ip[3] = Integer.parseInt(ips[3]);
  143.         return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
  144.     }

回复 "Java 常用字符串操作总结"

这儿你可以回复上面这条便签

captcha