標籤:
1. String轉ASCII碼
public static String stringToAscii(String value) { StringBuffer sbu = new StringBuffer(); char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (i <= chars.length - 1) { int ii = chars[i]; sbu.append(Integer.toHexString(ii)); } else { sbu.append((int) chars[i]); } } return sbu.toString(); }
2. ASCII碼轉String
public static String asciiToString(String value) { if("".equals(value) || value == null){ return ""; } StringBuffer sBuffer = new StringBuffer(); char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i = i + 2) { if (i < chars.length - 1) { int ii = Integer.valueOf( Character.toString(chars[i]) + Character.toString(chars[i + 1]), 16); sBuffer.append((char) (ii)); } } return sBuffer.toString(); }
3. 輸入資料流轉位元組數組
public static String inputStream2Byte(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return new String(bos.toByteArray(), "UTF-8"); }
4. 輸入資料流轉字串
public static String InputStreamTOString(InputStream in) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int count = -1; while ((count = in.read(data, 0, 2048)) != -1) outStream.write(data, 0, count); data = null; return new String(outStream.toByteArray()); }
5. 判斷輸入是不是包含中文
public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } public static boolean isChinese(String strName) { char[] ch = strName.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (isChinese(c)) { return true; } } return false; }
6. 檢驗子網路遮罩的合法性:子網路遮罩轉化為二進位之後不能全是1,也不能全是0,必須是連續的1或者連續的0,也就是說1和0不能交替出現。
實現方式,先將4個整數欄位按照每八位拼接起來,軟後轉化為二進位顯示方式,使用Regex進行匹配。
public static boolean checkMask(String maskStr){ String[] ips = maskStr.split("\\."); String binaryVal = ""; for (int i = 0; i < ips.length; i++) { String binaryStr = Integer.toBinaryString(Integer.parseInt(ips[i])); Integer times = 8 - binaryStr.length(); for(int j = 0; j < times; j++) { binaryStr = "0" + binaryStr; //補齊八位,每次需要進行八位合并 } binaryVal += binaryStr; } Pattern regxPattern = Pattern.compile("^[1]*[0]*$"); if(regxPattern.matcher(binaryVal).matches()) { return true; }else { return false; } }
7. 檢查IP的合法性,並轉化為整數表示
public static boolean validIP(String ip) { if (ip == null) { return false; } String IP_REGEX = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\." + "((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b"; Pattern ipPattern = Pattern.compile(IP_REGEX); Matcher matcher = ipPattern.matcher(ip); return matcher.matches(); } public static Integer ipString2Int(String strIp){ if(!validIP(strIp)) return null; int[] ip = new int[4]; String[] ips = strIp.split("\\."); ip[0] = Integer.parseInt(ips[0]); ip[1] = Integer.parseInt(ips[1]); ip[2] = Integer.parseInt(ips[2]); ip[3] = Integer.parseInt(ips[3]); return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; }
Java 常用字串操作總結