總結下工程中用到的字符串操做java
package com.cheqiren.caren.util; import java.util.Random; import java.util.UUID; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletResponse; import com.cheqiren.caren.common.CRConstants; public class CRUtils { public static String getCurrentDateByFormat(String format) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } public static String getUUID() { return UUID.randomUUID().toString().replaceAll("-", ""); } /** * 隨機字符串 * * @param length * 字符串長度 * @return 隨機字符串 */ public static String getRandomString(int length) { String str = "abcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; ++i) { int number = random.nextInt(36);// [0,62) sb.append(str.charAt(number)); } return sb.toString(); } /** * get時添加資源地址 * * @param path * 地址 * @return */ public static String addResourcePath(String path) { if (path != null && !(path.toLowerCase().contains("http://") || path .toLowerCase().contains("https://")) && path.contains("caren")) { return CRConstants.WEB_RESOURCE_URL + path; } return path; } /** * set時去掉資源地址 * * @param path * 地址 * @return */ public static String delResourcePath(String path) { path = path == null ? null : path.trim(); if (path.contains(CRConstants.WEB_RESOURCE_URL)) { path = path.replace(CRConstants.WEB_RESOURCE_URL, ""); } return path; } /** * 獲取字符串中第N次出現的字符位置 * * @param string * 字符串 * @param str * 字符 * @param pos * 第幾回 * @return 找到的位置 -1沒有找到 */ public static int getCharacterPosition(String string, String str, int pos) { Matcher slashMatcher = Pattern.compile(str).matcher(string); int rePos = -1; int mIdx = 0; while (slashMatcher.find()) { mIdx++; // 當"/"符號第三次出現的位置 if (mIdx == pos) { rePos = slashMatcher.start(); break; } } return rePos; } /** * 輸出數據 * * @param response * @param str * @param type */ public static void printl(HttpServletResponse response, String str, String type) { try { response.setCharacterEncoding("utf-8"); response.setContentType("text/" + type); PrintWriter out = response.getWriter(); str = str.replace("\\", ""); str = str.replaceAll("\r\n", ""); out.write(str); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Unicode轉String * * @param unicodeStr * 需轉化的字符串 * @return 轉換後的字符串 */ public static String unicodeToUTF8(String unicodeStr) { unicodeStr = (unicodeStr == null ? "" : unicodeStr); if (unicodeStr.indexOf("\\u") == -1)// 若是不是unicode碼則原樣返回 return unicodeStr; StringBuilder sb = new StringBuilder(); int i = -1; int pos = 0; while ((i = unicodeStr.indexOf("\\u", pos)) != -1) { sb.append(unicodeStr.substring(pos, i)); if (i + 5 < unicodeStr.length()) { pos = i + 6; sb.append((char) Integer.parseInt( unicodeStr.substring(i + 2, i + 6), 16)); } } return sb.toString(); } /** * 將字符串轉爲unicode * @param gbString 需轉化的字符串 * @return 轉換後的unicode字符串 */ public static String gbEncoding(final String gbString) { char[] utfBytes = gbString.toCharArray(); String unicodeBytes = ""; for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) { String hexB = Integer.toHexString(utfBytes[byteIndex]); if (hexB.length() <= 2) { hexB = "00" + hexB; } unicodeBytes = unicodeBytes + "\\u" + hexB; } System.out.println("unicodeBytes is: " + unicodeBytes); return unicodeBytes; } }