StringUtil详解
生活随笔
收集整理的這篇文章主要介紹了
StringUtil详解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、字符串達(dá)到多長(zhǎng)才截取
2、將指定的對(duì)象轉(zhuǎn)換為String類型
3、轉(zhuǎn)換字符,用于替換提交的數(shù)據(jù)中存在非法數(shù)據(jù):"'"
4、對(duì)標(biāo)題""轉(zhuǎn)換為中文“”采用對(duì)應(yīng)轉(zhuǎn)換
5、替換HTML標(biāo)記
6、標(biāo)題中含有特殊字符替換 如:●▲@◎※ 主要在標(biāo)題中使用
7、替換所有英文字母
8、替換所有數(shù)字
9、將/n轉(zhuǎn)換成為回車<br> ,空格轉(zhuǎn)為
10、清除所有<>標(biāo)記符號(hào) 主要在搜索中顯示文字內(nèi)容 而不顯示樣式
11、清楚WOrd垃圾代碼
12、判斷傳入的字符串如果為null則返回"",否則返回其本身
13、獲取百分比
[java] view plain copy
package com.xwtech.uomp.base.util; ?
??
import java.io.UnsupportedEncodingException; ?
import java.text.NumberFormat; ?
import java.util.HashMap; ?
import java.util.Map; ?
import java.util.regex.Matcher; ?
import java.util.regex.Pattern; ?
??
import com.xwtech.uomp.base.action.handler.HandlerResult; ?
import com.xwtech.uomp.base.constants.SystemCodeConstants; ?
??
public class StringUtil { ?
??
? ? public static final String arrTest[] = {"[br]", "[/b]", "[/i]", "[/u]", "[/size]", "[/color]", "[/align]", "[/url]", "[/email]", "[/img]"}; ?
? ? public static final String arrParam[] = {"\\[br\\]", "\\[b\\](.+?)\\[/b\\]", ?
? ? ? ? ? ? "\\[i\\](.+?)\\[/i\\]", ?
? ? ? ? ? ? "\\[u\\](.+?)\\[/u\\]", ?
? ? ? ? ? ? "\\[size=(.+?)\\](.+?)\\[/size\\]", ?
? ? ? ? ? ? "\\[color=(.+?)\\](.+?)\\[/color\\]", ?
? ? ? ? ? ? "\\[align=(.+?)\\](.+?)\\[/align\\]", ?
? ? ? ? ? ? "\\[url=(.+?)\\](.+?)\\[/url\\]", ?
? ? ? ? ? ? "\\[email=(.+?)\\](.+?)\\[/email\\]," + ?
? ? ? ? ? ? ? ? ? ? "\\[img=(.+?)\\](.+?)\\[/img\\]"}; ?
? ? public static final String arrCode[] = {"<br>", "<b>$1</b>", "<i>$1</i>", "<u>$1</u>", ?
? ? ? ? ? ? "<font size=\"$1\">$2</font>", ?
? ? ? ? ? ? "<font color=\"$1\">$2</font>", ?
? ? ? ? ? ? "<div align=\"$1\">$2</div>", ?
? ? ? ? ? ? "<a href=\"$1\" target=\"_blank\">$2</a>", ?
? ? ? ? ? ? "<a href=\"email:$1\">$2</a>", ?
? ? ? ? ? ? "<img src=\"$1\" border=0>$2</img>"}; ?
??
??
? ? public static int getInt(String content) { ?
? ? ? ? int intContent; ?
? ? ? ? try { ?
? ? ? ? ? ? intContent = Integer.parseInt(content); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? intContent = 0; ?
? ? ? ? } ?
? ? ? ? return intContent; ?
? ? } ?
??
? ? public static long getLong(String content) { ?
? ? ? ? long lngContent; ?
? ? ? ? try { ?
? ? ? ? ? ? lngContent = Long.parseLong(content); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? lngContent = 0L; ?
? ? ? ? } ?
? ? ? ? return lngContent; ?
? ? } ?
??
? ? /** 1?
? ? ?* @param str ? ?原字符串?
? ? ?* @param length 字符串達(dá)到多長(zhǎng)才截取?
? ? ?* @return?
? ? ?*/ ?
? ? @SuppressWarnings("static-access") ?
? ? public static String subStringToPoint(String str, int length, String more) { ?
??
? ? ? ? String reStr = ""; ?
??
? ? ? ? if (str.length() * 2 - 1 > length) { ?
??
? ? ? ? ? ? int reInt = 0; ?
??
? ? ? ? ? ? if (str == null) ?
??
? ? ? ? ? ? ? ? return ""; ?
??
? ? ? ? ? ? char[] tempChar = str.toCharArray(); ?
??
? ? ? ? ? ? for (int kk = 0; (kk < tempChar.length && length > reInt); kk++) { ?
??
? ? ? ? ? ? ? ? String s1 = str.valueOf(tempChar[kk]); ?
??
? ? ? ? ? ? ? ? byte[] b = s1.getBytes(); ?
??
? ? ? ? ? ? ? ? reInt += b.length; ?
??
? ? ? ? ? ? ? ? reStr += tempChar[kk]; ?
??
? ? ? ? ? ? } ?
??
? ? ? ? ? ? if (length == reInt || (length == reInt - 1)) { ?
??
? ? ? ? ? ? ? ? if (!reStr.equals(str)) { ?
? ? ? ? ? ? ? ? ? ? reStr += more; ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
??
? ? ? ? } else { ?
? ? ? ? ? ? reStr = str; ?
? ? ? ? } ?
? ? ? ? return reStr; ?
??
? ? } ?
??
??
? ? /** 2?
? ? ?* 將指定的對(duì)象轉(zhuǎn)換為String類型?
? ? ?*?
? ? ?* @param curObject 傳入對(duì)象參數(shù)?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String getString(Object curObject) { ?
? ? ? ? if (null == curObject) { ?
? ? ? ? ? ? throw new NullPointerException("The input object is null."); ?
? ? ? ? } else { ?
? ? ? ? ? ? return curObject.toString(); ?
? ? ? ? } ?
? ? } ?
??
? ? /** 3?
? ? ?* 轉(zhuǎn)換字符,用于替換提交的數(shù)據(jù)中存在非法數(shù)據(jù):"'"?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceChar(String content) { ?
? ? ? ? String newstr = ""; ?
? ? ? ? newstr = content.replaceAll("\'", "''"); ?
? ? ? ? return newstr; ?
? ? } ?
??
? ? /**4?
? ? ?* 對(duì)標(biāo)題""轉(zhuǎn)換為中文“”采用對(duì)應(yīng)轉(zhuǎn)換?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceSymbol(String content) { ?
? ? ? ? int intPlaceNum = 0; ?
? ? ? ? int Num = 0; ?
? ? ? ? String strContent = content; ?
? ? ? ? while (true) { ?
? ? ? ? ? ? //判斷是否還存在" ?
? ? ? ? ? ? intPlaceNum = strContent.indexOf("\""); ?
? ? ? ? ? ? if (intPlaceNum < 0) { ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? if (Num % 2 == 0) { ?
? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceFirst("\"", "“"); ?
? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceFirst("\"", "”"); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? Num = Num + 1; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**5?
? ? ?* 替換HTML標(biāo)記?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceCharToHtml(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("<", "<"); ?
? ? ? ? strContent = strContent.replaceAll(">", ">"); ?
? ? ? ? strContent = strContent.replaceAll("\"", """); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? public static String replaceHtmlToChar(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("<", "<"); ?
? ? ? ? strContent = strContent.replaceAll(">", ">"); ?
? ? ? ? strContent = strContent.replaceAll(""", "\""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? //數(shù)據(jù)庫(kù)替換 ?
? ? public static String replaceCharToSql(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("%", "\\\\%"); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? public static String toHtmlValue(String value) { ?
? ? ? ? if (null == value) { ?
? ? ? ? ? ? return null; ?
? ? ? ? } ?
? ? ? ? char a = 0; ?
? ? ? ? StringBuffer buf = new StringBuffer(); ?
? ? ? ? for (int i = 0; i < value.length(); i++) { ?
? ? ? ? ? ? a = value.charAt(i); ?
? ? ? ? ? ? switch (a) { ?
? ? ? ? ? ? ? ? // 雙引號(hào) ?
? ? ? ? ? ? ? ? case 34: ?
? ? ? ? ? ? ? ? ? ? buf.append("""); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // &號(hào) ?
? ? ? ? ? ? ? ? case 38: ?
? ? ? ? ? ? ? ? ? ? buf.append("&"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 單引號(hào) ?
? ? ? ? ? ? ? ? case 39: ?
? ? ? ? ? ? ? ? ? ? buf.append("'"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 小于號(hào) ?
? ? ? ? ? ? ? ? case 60: ?
? ? ? ? ? ? ? ? ? ? buf.append("<"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 大于號(hào) ?
? ? ? ? ? ? ? ? case 62: ?
? ? ? ? ? ? ? ? ? ? buf.append(">"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? default: ?
? ? ? ? ? ? ? ? ? ? buf.append(a); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return buf.toString(); ?
? ? } ?
??
??
? ? /**6?
? ? ?* 標(biāo)題中含有特殊字符替換 如:●▲@◎※ 主要在標(biāo)題中使用?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceSign(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll("\\*", ""); ?
? ? ? ? strContent = strContent.replaceAll("\\$", ""); ?
? ? ? ? strContent = strContent.replaceAll("\\+", ""); ?
? ? ? ? String arrStr[] = {":", ":", "●", "▲", "■", "@", "@", ?
? ? ? ? ? ? ? ? "◎", "★", "※", "#", "〓", "\", "§", "☆", ?
? ? ? ? ? ? ? ? "○", "◇", "◆", "□", "△", "&", "^", " ̄", ?
? ? ? ? ? ? ? ? "_", "♂", "♀", "Ю", "┭", "①", "「", "」", "≮", "§", ?
? ? ? ? ? ? ? ? "£", "∑", "『", "』", "⊙", "∷", "Θ", "の", "↓", "↑", ?
? ? ? ? ? ? ? ? "Ф", "~", "Ⅱ", "∈", "┣", "┫", "╋", "┇", "┋", "→", ?
? ? ? ? ? ? ? ? "←", "!", "Ж", "#"}; ?
? ? ? ? for (int i = 0; i < arrStr.length; i++) { ?
? ? ? ? ? ? if ((strContent.indexOf(arrStr[i])) >= 0) { ?
? ? ? ? ? ? ? ? strContent = strContent.replaceAll(arrStr[i], ""); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**7?
? ? ?* 替換所有英文字母?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceLetter(String content) { ?
? ? ? ? String strMark = "[^[A-Za-z]+$]"; ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll(strMark, ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**8?
? ? ?* 替換所有數(shù)字?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceNumber(String content) { ?
? ? ? ? String strMark = "[^[0-9]+$]"; ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll(strMark, ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**9?
? ? ?* 將/n轉(zhuǎn)換成為回車<br> ,空格轉(zhuǎn)為 ?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceBr(String content) { ?
? ? ? ? if (content == null) { ?
? ? ? ? ? ? return ""; ?
? ? ? ? } ?
? ? ? ? String strContent = ""; ?
??
? ? ? ? // String strMark ="[/\n\r\t]"; ?
??
? ? ? ? //strContent = content.replaceAll(strMark,"<br>"); ?
??
? ? ? ? strContent = content.replaceAll("\n\r\t", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\n\r", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\r\n", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\n", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\r", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll(" ", " "); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**10?
? ? ?* 清除所有<>標(biāo)記符號(hào) 主要在搜索中顯示文字內(nèi)容 而不顯示樣式?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceMark(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? String strMark = "<\\s*[^>]*>"; ?
? ? ? ? strContent = content.trim(); ?
? ? ? ? strContent = strContent.replaceAll("\"", ""); ?
? ? ? ? strContent = strContent.replaceAll("\'", ""); ?
? ? ? ? //刪除所有<>標(biāo)記 ?
? ? ? ? strContent = strContent.replaceAll(strMark, ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll("\r", ""); ?
? ? ? ? strContent = strContent.replaceAll("\n", ""); ?
? ? ? ? strContent = strContent.replaceAll("\r\n", ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**11?
? ? ?* 清楚WOrd垃圾代碼?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String clearWord(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.trim(); ?
? ? ? ? strContent = strContent.replaceAll("x:str", ""); ?
? ? ? ? //Remove Style attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) style=\"([^\"]*)\"", "<$1"); ?
? ? ? ? //Remove all SPAN ?tags ?
? ? ? ? strContent = strContent.replaceAll("<\\/?SPAN[^>]*>", ""); ?
? ? ? ? //Remove Lang attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) lang=([^ |>]*)([^>]*)", "<$1$3"); ?
? ? ? ? //Remove Class attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) class=([^ |>]*)([^>]*)", "<$1$3"); ?
? ? ? ? //Remove XML elements and declarations ?
? ? ? ? strContent = strContent.replaceAll("<\\\\?\\?xml[^>]*>", ""); ?
? ? ? ? //Remove Tags with XML namespace declarations: <o:p></o:p> ?
? ? ? ? strContent = strContent.replaceAll("<\\/?\\w+:[^>]*>", ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**?
? ? ?* 對(duì)組ID信息進(jìn)行處理 轉(zhuǎn)換為標(biāo)準(zhǔn)ID組 并過(guò)濾重復(fù)的信息?
? ? ?*?
? ? ?* @param teamId?
? ? ?* @return?
? ? ?*/ ?
? ? public static String checkTeamId(String teamId) { ?
? ? ? ? String strTeamId = ""; ?
? ? ? ? String strTempId = ""; ?
? ? ? ? String strTemp = ""; ?
? ? ? ? String[] arrTeamId = teamId.split(","); ?
? ? ? ? for (int num = 0; num < arrTeamId.length; num++) { ?
? ? ? ? ? ? strTemp = arrTeamId[num].trim(); ?
? ? ? ? ? ? if ((!strTemp.equals("")) && (!strTemp.equals("0"))) { ?
? ? ? ? ? ? ? ? if ((strTempId.indexOf("," + strTemp + ",")) >= 0) { //表示已經(jīng)保存過(guò)了 ?
? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? if (strTeamId.equals("")) { ?
? ? ? ? ? ? ? ? ? ? ? ? strTeamId = strTemp; ?
? ? ? ? ? ? ? ? ? ? ? ? strTempId = strTempId + "," + strTemp + ","; ?
? ? ? ? ? ? ? ? ? ? ? ? ; ?
? ? ? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? ? ? strTeamId = strTeamId + "," + strTemp; ?
? ? ? ? ? ? ? ? ? ? ? ? strTempId = strTempId + strTemp + ","; ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return strTeamId; ?
? ? } ?
??
??
? ? public static String replaceUbb(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? try { ?
? ? ? ? ? ? for (int num = 0; num < arrTest.length; num++) { ?
? ? ? ? ? ? ? ? if ((strContent.indexOf(arrTest[num])) >= 0) { ?
? ? ? ? ? ? ? ? ? ? try { ?
? ? ? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceAll(arrParam[num], arrCode[num]); ?
? ? ? ? ? ? ? ? ? ? } catch (Exception ex) { ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? //System.out.println("UBB CODE 錯(cuò)誤"+e); ?
? ? ? ? } ?
? ? ? ? return strContent; ?
? ? } ?
??
??
? ? /**12?
? ? ?* 判斷傳入的字符串如果為null則返回"",否則返回其本身?
? ? ?*?
? ? ?* @param string?
? ? ?* @param instant?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String convertNull(String string, String instant) { ?
? ? ? ? return isNull(string) ? instant : string; ?
? ? } ?
??
? ? /**?
? ? ?* {@link #convertNull(String, String)}?
? ? ?*?
? ? ?* @param string?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String convertNull(String string) { ?
? ? ? ? return convertNull(string, ""); ?
? ? } ?
??
? ? /**?
? ? ?* 判斷對(duì)象是否為空?
? ? ?*?
? ? ?* @param obj Object?
? ? ?* @return boolean 空返回true,非空返回false?
? ? ?*/ ?
? ? public static boolean isNull(Object obj) { ?
? ? ? ? return (null == obj) ? true : false; ?
? ? } ?
??
? ? /**?
? ? ?* Description:判斷字段空null <br>?
? ? ?*?
? ? ?* @param s?
? ? ?* @return boolean?
? ? ?*/ ?
? ? public static boolean isNull(String s) { ?
? ? ? ? if (s == null || "".equals(s.trim())) { ?
? ? ? ? ? ? return true; ?
? ? ? ? } ?
??
? ? ? ? return false; ?
? ? } ?
??
? ? /**13?
? ? ?* 獲取百分比?
? ? ?*?
? ? ?* @param p1?
? ? ?* @param p2?
? ? ?* @return?
? ? ?*/ ?
? ? public static String percent(double p1, double p2) { ?
? ? ? ? if (p2 == 0) { ?
? ? ? ? ? ? return "0.00%"; ?
? ? ? ? } ?
? ? ? ? String str; ?
? ? ? ? double p3 = p1 / p2; ?
? ? ? ? NumberFormat nf = NumberFormat.getPercentInstance(); ?
? ? ? ? nf.setMinimumFractionDigits(2); ?
? ? ? ? str = nf.format(p3); ?
? ? ? ? return str; ?
? ? } ?
??
? ? /**?
? ? ?* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法?
? ? ?*?
? ? ?* @param str ? ? ? ?待轉(zhuǎn)換編碼的字符串?
? ? ?* @param oldCharset 原編碼?
? ? ?* @param newCharset 目標(biāo)編碼?
? ? ?* @return?
? ? ?* @throws UnsupportedEncodingException?
? ? ?*/ ?
? ? public static String changeCharset(String str, String oldCharset, String newCharset) { ?
? ? ? ? try { ?
? ? ? ? ? ? if (str != null) { ?
? ? ? ? ? ? ? ? //用舊的字符編碼解碼字符串。解碼可能會(huì)出現(xiàn)異常。 ?
? ? ? ? ? ? ? ? byte[] bs = str.getBytes(oldCharset); ?
? ? ? ? ? ? ? ? //用新的字符編碼生成字符串 ?
? ? ? ? ? ? ? ? return new String(bs, newCharset); ?
? ? ? ? ? ? } ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? ? ? return ""; ?
? ? ? ? } ?
? ? ? ? return ""; ?
? ? } ?
??
? ? /**?
? ? ?* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法?
? ? ?*?
? ? ?* @param str ? ? ? ?待轉(zhuǎn)換編碼的字符串?
? ? ?* @param newCharset 目標(biāo)編碼?
? ? ?* @return?
? ? ?* @throws UnsupportedEncodingException?
? ? ?*/ ?
? ? public String changeCharset(String str, String newCharset) { ?
? ? ? ? try { ?
? ? ? ? ? ? if (str != null) { ?
? ? ? ? ? ? ? ? //用默認(rèn)字符編碼解碼字符串。 ?
? ? ? ? ? ? ? ? byte[] bs = str.getBytes(); ?
? ? ? ? ? ? ? ? //用新的字符編碼生成字符串 ?
? ? ? ? ? ? ? ? return new String(bs, newCharset); ?
? ? ? ? ? ? } ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? ? ? return ""; ?
? ? } ?
??
? ? /**?
? ? ?* 解析html中的參數(shù)信息?
? ? ?*?
? ? ?* @param elementStr?
? ? ?* @return?
? ? ?*/ ?
? ? public static Map<String, String> getConfigValue(String elementStr) { ?
? ? ? ? try { ?
? ? ? ? ? ? elementStr = java.net.URLDecoder.decode(elementStr, "utf-8"); ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? ? ? int start = elementStr.indexOf("configvalue"); ?
? ? ? ? Map<String, String> map = null; //參數(shù)的鍵值對(duì) ?
? ? ? ? if (start != -1) { ?
? ? ? ? ? ? map = new HashMap<String, String>(); ?
? ? ? ? ? ? start = elementStr.indexOf("\"", start); ?
? ? ? ? ? ? int end = elementStr.lastIndexOf("||"); ?
? ? ? ? ? ? if (start < 0 || end < 0) { ?
? ? ? ? ? ? ? ? return null; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? String configValue = elementStr.substring(start + 1, end); ?
? ? ? ? ? ? String[] values = configValue.split("\\|\\|"); ?
??
? ? ? ? ? ? for (int i = 0; i < values.length; i++) { ?
? ? ? ? ? ? ? ? String value = values[i]; ?
? ? ? ? ? ? ? ? if (value != null && value.trim().length() > 1) { ?
? ? ? ? ? ? ? ? ? ? int de = value.indexOf("="); ?
? ? ? ? ? ? ? ? ? ? if (de > 0) { ?
? ? ? ? ? ? ? ? ? ? ? ? String name = value.substring(0, de); ?
? ? ? ? ? ? ? ? ? ? ? ? String v = value.substring(de + 1); ?
? ? ? ? ? ? ? ? ? ? ? ? map.put(name, v); ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return map; ?
? ? } ?
??
? ? /**?
? ? ?* 轉(zhuǎn)換空值為0?
? ? ?*?
? ? ?* @param str?
? ? ?* @return?
? ? ?*/ ?
? ? public static String conventString(String str) { ?
? ? ? ? return null == str || "".equals(str) ? "" + "0" : str; ?
? ? } ?
??
? ? public static String alert(HandlerResult result, String contextPath) { ?
? ? ? ? StringBuffer sf = new StringBuffer(); ?
? ? ? ? sf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); ?
? ? ? ? sf.append("<html><head><title>信息提示</title>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/css/frame.css\" ?/>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/skins/dhtmlxwindows_dhx_skyblue.css\"/>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/dhtmlxwindows.css\"/>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxLayout/codebase/dhtmlxcommon.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxLayout/codebase/dhtmlxcontainer.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/dhtmlxwindows.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/jquery-1.7.1.min.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/uompDialog.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/main.js\" ></script>"); ?
? ? ? ? sf.append("</head><body>"); ?
? ? ? ? sf.append("</body></html>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\">"); ?
? ? ? ? sf.append("$(document).ready(function(){"); ?
? ? ? ? sf.append(" top.UOMPDialog.alert('" + result.getResMsg() + "',0,\"\""); ?
? ? ? ? if (SystemCodeConstants.NOT_LOGIN.equals(result.getSysCode())) { ?
? ? ? ? ? ? sf.append(", {'yes' : function(){"); ?
? ? ? ? ? ? sf.append(" top.location.href = '").append(contextPath).append("/index.jsp'").append(";"); ?
? ? ? ? ? ? sf.append("}}"); ?
? ? ? ? } ?
? ? ? ? sf.append(");"); ?
? ? ? ? sf.append(" });"); ?
? ? ? ? sf.append("</script>"); ?
? ? ? ? return sf.toString(); ?
? ? } ?
??
? ? public static void main(String[] args) { ?
? ? ? ? Pattern pattern = Pattern.compile("<span\\s.+?]]</span>"); ?
? ? ? ? String str = "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)名稱:<span configvalue=\"eid=1042||ename=%E4%B8%9A%E5%8A%A1%E5%90%8D%E7%A7%B0||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)名稱 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)資費(fèi):<span configvalue=\"eid=1041||ename=%E4%B8%9A%E5%8A%A1%E8%B5%84%E8%B4%B9||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)資費(fèi) ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)介紹:<br />" ?
? ? ? ? ? ? ? ? + "<span configvalue=\"eid=1040||ename=%E4%B8%9A%E5%8A%A1%E4%BB%8B%E7%BB%8D||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)介紹 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "<br />" ?
? ? ? ? ? ? ? ? + "<br />" ?
? ? ? ? ? ? ? ? + "<br /><span >]]</span>" ?
? ? ? ? ? ? ? ? + "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />"; ?
? ? ? ? Matcher matcher = pattern.matcher(str); ?
? ? ? ? String htmlStr = ""; ?
? ? ? ? StringBuffer strbuff = new StringBuffer(); ?
??
? ? ? ? int i = 0; ?
? ? ? ? while (matcher.find()) { ?
? ? ? ? ? ? String bm = matcher.group(); ?
? ? ? ? ? ? System.out.println(bm); ?
? ? ? ? ? ? Map map = getConfigValue(bm); ?
? ? ? ? ? ? if (map != null) { ?
? ? ? ? ? ? ? ? //todo:從緩存中獲取數(shù)據(jù) ?
? ? ? ? ? ? ? ? if (((String) map.get("eid")).equals("1043")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 1"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1042")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 2"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1041")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】 + 3"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1040")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 4"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1046")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+888888888888"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? i++; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? matcher.appendTail(strbuff); ?
? ? ? ? htmlStr += strbuff.toString(); ?
? ? ? ? System.out.println(htmlStr + "=================" + i); ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 方法描述:判斷傳入的字符串是否非空,即:字符串是否等于null、""或" "。?
? ? ?* 創(chuàng)建日期:2013-12-7下午11:39:34?
? ? ?* 修改日期:?
? ? ?* 作者:zhanglu?
? ? ?* @param:?
? ? ?* @return:boolean?
? ? ?*/ ?
? ? public static boolean isNotEmpty(String str) ?
? ? { ?
? ? ? ? if ((null == str) || ("".equals(str.trim()))) ?
? ? ? ? { ?
? ? ? ? ? ? return false; ?
? ? ? ? } ?
? ? ? ? else ?
? ? ? ? { ?
? ? ? ? ? ? return true; ?
? ? ? ? } ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 方法描述:將傳入的字符串轉(zhuǎn)換成整型數(shù)據(jù),如果轉(zhuǎn)換過(guò)程中發(fā)生異常,則返回默認(rèn)值:defaultValue。?
? ? ?* 創(chuàng)建日期:2013-12-7下午11:40:20?
? ? ?* 修改日期:?
? ? ?* 作者:zhanglu?
? ? ?* @param:?
? ? ?* @return:int?
? ? ?*/ ?
? ? public static int convertIntoInt(String str, int defaultValue) ?
? ? { ?
? ? ? ? // 定義一個(gè)返回值,假如轉(zhuǎn)型過(guò)程中發(fā)生異常,則返回此默認(rèn)值 ?
? ? ? ? int retData = defaultValue; ?
? ? ? ? try ?
? ? ? ? { ?
? ? ? ? ? ? retData = Integer.parseInt(str.trim()); ?
? ? ? ? } ?
? ? ? ? catch(NumberFormatException e) ?
? ? ? ? { ?
? ? ? ? } ?
? ? ? ? catch(Exception ex) ?
? ? ? ? { ?
? ? ? ? } ?
??
? ? ? ? return retData; ?
? ? } ?
??
}
2、將指定的對(duì)象轉(zhuǎn)換為String類型
3、轉(zhuǎn)換字符,用于替換提交的數(shù)據(jù)中存在非法數(shù)據(jù):"'"
4、對(duì)標(biāo)題""轉(zhuǎn)換為中文“”采用對(duì)應(yīng)轉(zhuǎn)換
5、替換HTML標(biāo)記
6、標(biāo)題中含有特殊字符替換 如:●▲@◎※ 主要在標(biāo)題中使用
7、替換所有英文字母
8、替換所有數(shù)字
9、將/n轉(zhuǎn)換成為回車<br> ,空格轉(zhuǎn)為
10、清除所有<>標(biāo)記符號(hào) 主要在搜索中顯示文字內(nèi)容 而不顯示樣式
11、清楚WOrd垃圾代碼
12、判斷傳入的字符串如果為null則返回"",否則返回其本身
13、獲取百分比
[java] view plain copy
package com.xwtech.uomp.base.util; ?
??
import java.io.UnsupportedEncodingException; ?
import java.text.NumberFormat; ?
import java.util.HashMap; ?
import java.util.Map; ?
import java.util.regex.Matcher; ?
import java.util.regex.Pattern; ?
??
import com.xwtech.uomp.base.action.handler.HandlerResult; ?
import com.xwtech.uomp.base.constants.SystemCodeConstants; ?
??
public class StringUtil { ?
??
? ? public static final String arrTest[] = {"[br]", "[/b]", "[/i]", "[/u]", "[/size]", "[/color]", "[/align]", "[/url]", "[/email]", "[/img]"}; ?
? ? public static final String arrParam[] = {"\\[br\\]", "\\[b\\](.+?)\\[/b\\]", ?
? ? ? ? ? ? "\\[i\\](.+?)\\[/i\\]", ?
? ? ? ? ? ? "\\[u\\](.+?)\\[/u\\]", ?
? ? ? ? ? ? "\\[size=(.+?)\\](.+?)\\[/size\\]", ?
? ? ? ? ? ? "\\[color=(.+?)\\](.+?)\\[/color\\]", ?
? ? ? ? ? ? "\\[align=(.+?)\\](.+?)\\[/align\\]", ?
? ? ? ? ? ? "\\[url=(.+?)\\](.+?)\\[/url\\]", ?
? ? ? ? ? ? "\\[email=(.+?)\\](.+?)\\[/email\\]," + ?
? ? ? ? ? ? ? ? ? ? "\\[img=(.+?)\\](.+?)\\[/img\\]"}; ?
? ? public static final String arrCode[] = {"<br>", "<b>$1</b>", "<i>$1</i>", "<u>$1</u>", ?
? ? ? ? ? ? "<font size=\"$1\">$2</font>", ?
? ? ? ? ? ? "<font color=\"$1\">$2</font>", ?
? ? ? ? ? ? "<div align=\"$1\">$2</div>", ?
? ? ? ? ? ? "<a href=\"$1\" target=\"_blank\">$2</a>", ?
? ? ? ? ? ? "<a href=\"email:$1\">$2</a>", ?
? ? ? ? ? ? "<img src=\"$1\" border=0>$2</img>"}; ?
??
??
? ? public static int getInt(String content) { ?
? ? ? ? int intContent; ?
? ? ? ? try { ?
? ? ? ? ? ? intContent = Integer.parseInt(content); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? intContent = 0; ?
? ? ? ? } ?
? ? ? ? return intContent; ?
? ? } ?
??
? ? public static long getLong(String content) { ?
? ? ? ? long lngContent; ?
? ? ? ? try { ?
? ? ? ? ? ? lngContent = Long.parseLong(content); ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? lngContent = 0L; ?
? ? ? ? } ?
? ? ? ? return lngContent; ?
? ? } ?
??
? ? /** 1?
? ? ?* @param str ? ?原字符串?
? ? ?* @param length 字符串達(dá)到多長(zhǎng)才截取?
? ? ?* @return?
? ? ?*/ ?
? ? @SuppressWarnings("static-access") ?
? ? public static String subStringToPoint(String str, int length, String more) { ?
??
? ? ? ? String reStr = ""; ?
??
? ? ? ? if (str.length() * 2 - 1 > length) { ?
??
? ? ? ? ? ? int reInt = 0; ?
??
? ? ? ? ? ? if (str == null) ?
??
? ? ? ? ? ? ? ? return ""; ?
??
? ? ? ? ? ? char[] tempChar = str.toCharArray(); ?
??
? ? ? ? ? ? for (int kk = 0; (kk < tempChar.length && length > reInt); kk++) { ?
??
? ? ? ? ? ? ? ? String s1 = str.valueOf(tempChar[kk]); ?
??
? ? ? ? ? ? ? ? byte[] b = s1.getBytes(); ?
??
? ? ? ? ? ? ? ? reInt += b.length; ?
??
? ? ? ? ? ? ? ? reStr += tempChar[kk]; ?
??
? ? ? ? ? ? } ?
??
? ? ? ? ? ? if (length == reInt || (length == reInt - 1)) { ?
??
? ? ? ? ? ? ? ? if (!reStr.equals(str)) { ?
? ? ? ? ? ? ? ? ? ? reStr += more; ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
??
? ? ? ? } else { ?
? ? ? ? ? ? reStr = str; ?
? ? ? ? } ?
? ? ? ? return reStr; ?
??
? ? } ?
??
??
? ? /** 2?
? ? ?* 將指定的對(duì)象轉(zhuǎn)換為String類型?
? ? ?*?
? ? ?* @param curObject 傳入對(duì)象參數(shù)?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String getString(Object curObject) { ?
? ? ? ? if (null == curObject) { ?
? ? ? ? ? ? throw new NullPointerException("The input object is null."); ?
? ? ? ? } else { ?
? ? ? ? ? ? return curObject.toString(); ?
? ? ? ? } ?
? ? } ?
??
? ? /** 3?
? ? ?* 轉(zhuǎn)換字符,用于替換提交的數(shù)據(jù)中存在非法數(shù)據(jù):"'"?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceChar(String content) { ?
? ? ? ? String newstr = ""; ?
? ? ? ? newstr = content.replaceAll("\'", "''"); ?
? ? ? ? return newstr; ?
? ? } ?
??
? ? /**4?
? ? ?* 對(duì)標(biāo)題""轉(zhuǎn)換為中文“”采用對(duì)應(yīng)轉(zhuǎn)換?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceSymbol(String content) { ?
? ? ? ? int intPlaceNum = 0; ?
? ? ? ? int Num = 0; ?
? ? ? ? String strContent = content; ?
? ? ? ? while (true) { ?
? ? ? ? ? ? //判斷是否還存在" ?
? ? ? ? ? ? intPlaceNum = strContent.indexOf("\""); ?
? ? ? ? ? ? if (intPlaceNum < 0) { ?
? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? if (Num % 2 == 0) { ?
? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceFirst("\"", "“"); ?
? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceFirst("\"", "”"); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? Num = Num + 1; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**5?
? ? ?* 替換HTML標(biāo)記?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceCharToHtml(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("<", "<"); ?
? ? ? ? strContent = strContent.replaceAll(">", ">"); ?
? ? ? ? strContent = strContent.replaceAll("\"", """); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? public static String replaceHtmlToChar(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("<", "<"); ?
? ? ? ? strContent = strContent.replaceAll(">", ">"); ?
? ? ? ? strContent = strContent.replaceAll(""", "\""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? //數(shù)據(jù)庫(kù)替換 ?
? ? public static String replaceCharToSql(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? strContent = strContent.replaceAll("%", "\\\\%"); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? public static String toHtmlValue(String value) { ?
? ? ? ? if (null == value) { ?
? ? ? ? ? ? return null; ?
? ? ? ? } ?
? ? ? ? char a = 0; ?
? ? ? ? StringBuffer buf = new StringBuffer(); ?
? ? ? ? for (int i = 0; i < value.length(); i++) { ?
? ? ? ? ? ? a = value.charAt(i); ?
? ? ? ? ? ? switch (a) { ?
? ? ? ? ? ? ? ? // 雙引號(hào) ?
? ? ? ? ? ? ? ? case 34: ?
? ? ? ? ? ? ? ? ? ? buf.append("""); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // &號(hào) ?
? ? ? ? ? ? ? ? case 38: ?
? ? ? ? ? ? ? ? ? ? buf.append("&"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 單引號(hào) ?
? ? ? ? ? ? ? ? case 39: ?
? ? ? ? ? ? ? ? ? ? buf.append("'"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 小于號(hào) ?
? ? ? ? ? ? ? ? case 60: ?
? ? ? ? ? ? ? ? ? ? buf.append("<"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? // 大于號(hào) ?
? ? ? ? ? ? ? ? case 62: ?
? ? ? ? ? ? ? ? ? ? buf.append(">"); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? ? ? default: ?
? ? ? ? ? ? ? ? ? ? buf.append(a); ?
? ? ? ? ? ? ? ? ? ? break; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return buf.toString(); ?
? ? } ?
??
??
? ? /**6?
? ? ?* 標(biāo)題中含有特殊字符替換 如:●▲@◎※ 主要在標(biāo)題中使用?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceSign(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll("\\*", ""); ?
? ? ? ? strContent = strContent.replaceAll("\\$", ""); ?
? ? ? ? strContent = strContent.replaceAll("\\+", ""); ?
? ? ? ? String arrStr[] = {":", ":", "●", "▲", "■", "@", "@", ?
? ? ? ? ? ? ? ? "◎", "★", "※", "#", "〓", "\", "§", "☆", ?
? ? ? ? ? ? ? ? "○", "◇", "◆", "□", "△", "&", "^", " ̄", ?
? ? ? ? ? ? ? ? "_", "♂", "♀", "Ю", "┭", "①", "「", "」", "≮", "§", ?
? ? ? ? ? ? ? ? "£", "∑", "『", "』", "⊙", "∷", "Θ", "の", "↓", "↑", ?
? ? ? ? ? ? ? ? "Ф", "~", "Ⅱ", "∈", "┣", "┫", "╋", "┇", "┋", "→", ?
? ? ? ? ? ? ? ? "←", "!", "Ж", "#"}; ?
? ? ? ? for (int i = 0; i < arrStr.length; i++) { ?
? ? ? ? ? ? if ((strContent.indexOf(arrStr[i])) >= 0) { ?
? ? ? ? ? ? ? ? strContent = strContent.replaceAll(arrStr[i], ""); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**7?
? ? ?* 替換所有英文字母?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceLetter(String content) { ?
? ? ? ? String strMark = "[^[A-Za-z]+$]"; ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll(strMark, ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**8?
? ? ?* 替換所有數(shù)字?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceNumber(String content) { ?
? ? ? ? String strMark = "[^[0-9]+$]"; ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.replaceAll(strMark, ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**9?
? ? ?* 將/n轉(zhuǎn)換成為回車<br> ,空格轉(zhuǎn)為 ?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceBr(String content) { ?
? ? ? ? if (content == null) { ?
? ? ? ? ? ? return ""; ?
? ? ? ? } ?
? ? ? ? String strContent = ""; ?
??
? ? ? ? // String strMark ="[/\n\r\t]"; ?
??
? ? ? ? //strContent = content.replaceAll(strMark,"<br>"); ?
??
? ? ? ? strContent = content.replaceAll("\n\r\t", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\n\r", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\r\n", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\n", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll("\r", "<br>"); ?
? ? ? ? strContent = strContent.replaceAll(" ", " "); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**10?
? ? ?* 清除所有<>標(biāo)記符號(hào) 主要在搜索中顯示文字內(nèi)容 而不顯示樣式?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String replaceMark(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? String strMark = "<\\s*[^>]*>"; ?
? ? ? ? strContent = content.trim(); ?
? ? ? ? strContent = strContent.replaceAll("\"", ""); ?
? ? ? ? strContent = strContent.replaceAll("\'", ""); ?
? ? ? ? //刪除所有<>標(biāo)記 ?
? ? ? ? strContent = strContent.replaceAll(strMark, ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll(" ", ""); ?
? ? ? ? strContent = strContent.replaceAll("\r", ""); ?
? ? ? ? strContent = strContent.replaceAll("\n", ""); ?
? ? ? ? strContent = strContent.replaceAll("\r\n", ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**11?
? ? ?* 清楚WOrd垃圾代碼?
? ? ?*?
? ? ?* @param Content?
? ? ?* @return?
? ? ?*/ ?
? ? public static String clearWord(String content) { ?
? ? ? ? String strContent = ""; ?
? ? ? ? strContent = content.trim(); ?
? ? ? ? strContent = strContent.replaceAll("x:str", ""); ?
? ? ? ? //Remove Style attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) style=\"([^\"]*)\"", "<$1"); ?
? ? ? ? //Remove all SPAN ?tags ?
? ? ? ? strContent = strContent.replaceAll("<\\/?SPAN[^>]*>", ""); ?
? ? ? ? //Remove Lang attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) lang=([^ |>]*)([^>]*)", "<$1$3"); ?
? ? ? ? //Remove Class attributes ?
? ? ? ? strContent = strContent.replaceAll("<(\\w[^>]*) class=([^ |>]*)([^>]*)", "<$1$3"); ?
? ? ? ? //Remove XML elements and declarations ?
? ? ? ? strContent = strContent.replaceAll("<\\\\?\\?xml[^>]*>", ""); ?
? ? ? ? //Remove Tags with XML namespace declarations: <o:p></o:p> ?
? ? ? ? strContent = strContent.replaceAll("<\\/?\\w+:[^>]*>", ""); ?
? ? ? ? return strContent; ?
? ? } ?
??
? ? /**?
? ? ?* 對(duì)組ID信息進(jìn)行處理 轉(zhuǎn)換為標(biāo)準(zhǔn)ID組 并過(guò)濾重復(fù)的信息?
? ? ?*?
? ? ?* @param teamId?
? ? ?* @return?
? ? ?*/ ?
? ? public static String checkTeamId(String teamId) { ?
? ? ? ? String strTeamId = ""; ?
? ? ? ? String strTempId = ""; ?
? ? ? ? String strTemp = ""; ?
? ? ? ? String[] arrTeamId = teamId.split(","); ?
? ? ? ? for (int num = 0; num < arrTeamId.length; num++) { ?
? ? ? ? ? ? strTemp = arrTeamId[num].trim(); ?
? ? ? ? ? ? if ((!strTemp.equals("")) && (!strTemp.equals("0"))) { ?
? ? ? ? ? ? ? ? if ((strTempId.indexOf("," + strTemp + ",")) >= 0) { //表示已經(jīng)保存過(guò)了 ?
? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? if (strTeamId.equals("")) { ?
? ? ? ? ? ? ? ? ? ? ? ? strTeamId = strTemp; ?
? ? ? ? ? ? ? ? ? ? ? ? strTempId = strTempId + "," + strTemp + ","; ?
? ? ? ? ? ? ? ? ? ? ? ? ; ?
? ? ? ? ? ? ? ? ? ? } else { ?
? ? ? ? ? ? ? ? ? ? ? ? strTeamId = strTeamId + "," + strTemp; ?
? ? ? ? ? ? ? ? ? ? ? ? strTempId = strTempId + strTemp + ","; ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return strTeamId; ?
? ? } ?
??
??
? ? public static String replaceUbb(String content) { ?
? ? ? ? String strContent = content; ?
? ? ? ? try { ?
? ? ? ? ? ? for (int num = 0; num < arrTest.length; num++) { ?
? ? ? ? ? ? ? ? if ((strContent.indexOf(arrTest[num])) >= 0) { ?
? ? ? ? ? ? ? ? ? ? try { ?
? ? ? ? ? ? ? ? ? ? ? ? strContent = strContent.replaceAll(arrParam[num], arrCode[num]); ?
? ? ? ? ? ? ? ? ? ? } catch (Exception ex) { ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? //System.out.println("UBB CODE 錯(cuò)誤"+e); ?
? ? ? ? } ?
? ? ? ? return strContent; ?
? ? } ?
??
??
? ? /**12?
? ? ?* 判斷傳入的字符串如果為null則返回"",否則返回其本身?
? ? ?*?
? ? ?* @param string?
? ? ?* @param instant?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String convertNull(String string, String instant) { ?
? ? ? ? return isNull(string) ? instant : string; ?
? ? } ?
??
? ? /**?
? ? ?* {@link #convertNull(String, String)}?
? ? ?*?
? ? ?* @param string?
? ? ?* @return String?
? ? ?*/ ?
? ? public static String convertNull(String string) { ?
? ? ? ? return convertNull(string, ""); ?
? ? } ?
??
? ? /**?
? ? ?* 判斷對(duì)象是否為空?
? ? ?*?
? ? ?* @param obj Object?
? ? ?* @return boolean 空返回true,非空返回false?
? ? ?*/ ?
? ? public static boolean isNull(Object obj) { ?
? ? ? ? return (null == obj) ? true : false; ?
? ? } ?
??
? ? /**?
? ? ?* Description:判斷字段空null <br>?
? ? ?*?
? ? ?* @param s?
? ? ?* @return boolean?
? ? ?*/ ?
? ? public static boolean isNull(String s) { ?
? ? ? ? if (s == null || "".equals(s.trim())) { ?
? ? ? ? ? ? return true; ?
? ? ? ? } ?
??
? ? ? ? return false; ?
? ? } ?
??
? ? /**13?
? ? ?* 獲取百分比?
? ? ?*?
? ? ?* @param p1?
? ? ?* @param p2?
? ? ?* @return?
? ? ?*/ ?
? ? public static String percent(double p1, double p2) { ?
? ? ? ? if (p2 == 0) { ?
? ? ? ? ? ? return "0.00%"; ?
? ? ? ? } ?
? ? ? ? String str; ?
? ? ? ? double p3 = p1 / p2; ?
? ? ? ? NumberFormat nf = NumberFormat.getPercentInstance(); ?
? ? ? ? nf.setMinimumFractionDigits(2); ?
? ? ? ? str = nf.format(p3); ?
? ? ? ? return str; ?
? ? } ?
??
? ? /**?
? ? ?* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法?
? ? ?*?
? ? ?* @param str ? ? ? ?待轉(zhuǎn)換編碼的字符串?
? ? ?* @param oldCharset 原編碼?
? ? ?* @param newCharset 目標(biāo)編碼?
? ? ?* @return?
? ? ?* @throws UnsupportedEncodingException?
? ? ?*/ ?
? ? public static String changeCharset(String str, String oldCharset, String newCharset) { ?
? ? ? ? try { ?
? ? ? ? ? ? if (str != null) { ?
? ? ? ? ? ? ? ? //用舊的字符編碼解碼字符串。解碼可能會(huì)出現(xiàn)異常。 ?
? ? ? ? ? ? ? ? byte[] bs = str.getBytes(oldCharset); ?
? ? ? ? ? ? ? ? //用新的字符編碼生成字符串 ?
? ? ? ? ? ? ? ? return new String(bs, newCharset); ?
? ? ? ? ? ? } ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? ? ? return ""; ?
? ? ? ? } ?
? ? ? ? return ""; ?
? ? } ?
??
? ? /**?
? ? ?* 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法?
? ? ?*?
? ? ?* @param str ? ? ? ?待轉(zhuǎn)換編碼的字符串?
? ? ?* @param newCharset 目標(biāo)編碼?
? ? ?* @return?
? ? ?* @throws UnsupportedEncodingException?
? ? ?*/ ?
? ? public String changeCharset(String str, String newCharset) { ?
? ? ? ? try { ?
? ? ? ? ? ? if (str != null) { ?
? ? ? ? ? ? ? ? //用默認(rèn)字符編碼解碼字符串。 ?
? ? ? ? ? ? ? ? byte[] bs = str.getBytes(); ?
? ? ? ? ? ? ? ? //用新的字符編碼生成字符串 ?
? ? ? ? ? ? ? ? return new String(bs, newCharset); ?
? ? ? ? ? ? } ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? ? ? return ""; ?
? ? } ?
??
? ? /**?
? ? ?* 解析html中的參數(shù)信息?
? ? ?*?
? ? ?* @param elementStr?
? ? ?* @return?
? ? ?*/ ?
? ? public static Map<String, String> getConfigValue(String elementStr) { ?
? ? ? ? try { ?
? ? ? ? ? ? elementStr = java.net.URLDecoder.decode(elementStr, "utf-8"); ?
? ? ? ? } catch (UnsupportedEncodingException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? ? ? int start = elementStr.indexOf("configvalue"); ?
? ? ? ? Map<String, String> map = null; //參數(shù)的鍵值對(duì) ?
? ? ? ? if (start != -1) { ?
? ? ? ? ? ? map = new HashMap<String, String>(); ?
? ? ? ? ? ? start = elementStr.indexOf("\"", start); ?
? ? ? ? ? ? int end = elementStr.lastIndexOf("||"); ?
? ? ? ? ? ? if (start < 0 || end < 0) { ?
? ? ? ? ? ? ? ? return null; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? String configValue = elementStr.substring(start + 1, end); ?
? ? ? ? ? ? String[] values = configValue.split("\\|\\|"); ?
??
? ? ? ? ? ? for (int i = 0; i < values.length; i++) { ?
? ? ? ? ? ? ? ? String value = values[i]; ?
? ? ? ? ? ? ? ? if (value != null && value.trim().length() > 1) { ?
? ? ? ? ? ? ? ? ? ? int de = value.indexOf("="); ?
? ? ? ? ? ? ? ? ? ? if (de > 0) { ?
? ? ? ? ? ? ? ? ? ? ? ? String name = value.substring(0, de); ?
? ? ? ? ? ? ? ? ? ? ? ? String v = value.substring(de + 1); ?
? ? ? ? ? ? ? ? ? ? ? ? map.put(name, v); ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return map; ?
? ? } ?
??
? ? /**?
? ? ?* 轉(zhuǎn)換空值為0?
? ? ?*?
? ? ?* @param str?
? ? ?* @return?
? ? ?*/ ?
? ? public static String conventString(String str) { ?
? ? ? ? return null == str || "".equals(str) ? "" + "0" : str; ?
? ? } ?
??
? ? public static String alert(HandlerResult result, String contextPath) { ?
? ? ? ? StringBuffer sf = new StringBuffer(); ?
? ? ? ? sf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); ?
? ? ? ? sf.append("<html><head><title>信息提示</title>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/css/frame.css\" ?/>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/skins/dhtmlxwindows_dhx_skyblue.css\"/>"); ?
? ? ? ? sf.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/dhtmlxwindows.css\"/>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxLayout/codebase/dhtmlxcommon.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxLayout/codebase/dhtmlxcontainer.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/Dhtmlx/dhtmlxSuite/dhtmlxWindows/codebase/dhtmlxwindows.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/jquery-1.7.1.min.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/uompDialog.js\"></script>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\" language=\"javascript\" src=\"").append(contextPath).append("/resource/scripts/main.js\" ></script>"); ?
? ? ? ? sf.append("</head><body>"); ?
? ? ? ? sf.append("</body></html>"); ?
? ? ? ? sf.append("<script type=\"text/javascript\">"); ?
? ? ? ? sf.append("$(document).ready(function(){"); ?
? ? ? ? sf.append(" top.UOMPDialog.alert('" + result.getResMsg() + "',0,\"\""); ?
? ? ? ? if (SystemCodeConstants.NOT_LOGIN.equals(result.getSysCode())) { ?
? ? ? ? ? ? sf.append(", {'yes' : function(){"); ?
? ? ? ? ? ? sf.append(" top.location.href = '").append(contextPath).append("/index.jsp'").append(";"); ?
? ? ? ? ? ? sf.append("}}"); ?
? ? ? ? } ?
? ? ? ? sf.append(");"); ?
? ? ? ? sf.append(" });"); ?
? ? ? ? sf.append("</script>"); ?
? ? ? ? return sf.toString(); ?
? ? } ?
??
? ? public static void main(String[] args) { ?
? ? ? ? Pattern pattern = Pattern.compile("<span\\s.+?]]</span>"); ?
? ? ? ? String str = "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)名稱:<span configvalue=\"eid=1042||ename=%E4%B8%9A%E5%8A%A1%E5%90%8D%E7%A7%B0||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)名稱 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)資費(fèi):<span configvalue=\"eid=1041||ename=%E4%B8%9A%E5%8A%A1%E8%B5%84%E8%B4%B9||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)資費(fèi) ]]</span><br />" ?
? ? ? ? ? ? ? ? + "業(yè)務(wù)介紹:<br />" ?
? ? ? ? ? ? ? ? + "<span configvalue=\"eid=1040||ename=%E4%B8%9A%E5%8A%A1%E4%BB%8B%E7%BB%8D||busiNum=CL||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 業(yè)務(wù)介紹 ]]</span><br />" ?
? ? ? ? ? ? ? ? + "<br />" ?
? ? ? ? ? ? ? ? + "<br />" ?
? ? ? ? ? ? ? ? + "<br /><span >]]</span>" ?
? ? ? ? ? ? ? ? + "<span configvalue=\"eid=1043||ename=%E9%9D%A2%E5%8C%85%E5%B1%91||folderId=CLBLYM||\" contenteditable=\"false\" style=\"background-color: #ffff00; color: #000000\">[[ 面包屑 ]]</span><br />"; ?
? ? ? ? Matcher matcher = pattern.matcher(str); ?
? ? ? ? String htmlStr = ""; ?
? ? ? ? StringBuffer strbuff = new StringBuffer(); ?
??
? ? ? ? int i = 0; ?
? ? ? ? while (matcher.find()) { ?
? ? ? ? ? ? String bm = matcher.group(); ?
? ? ? ? ? ? System.out.println(bm); ?
? ? ? ? ? ? Map map = getConfigValue(bm); ?
? ? ? ? ? ? if (map != null) { ?
? ? ? ? ? ? ? ? //todo:從緩存中獲取數(shù)據(jù) ?
? ? ? ? ? ? ? ? if (((String) map.get("eid")).equals("1043")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 1"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1042")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 2"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1041")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】 + 3"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1040")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+ 4"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } else if (((String) map.get("eid")).equals("1046")) { ?
? ? ? ? ? ? ? ? ? ? String se = "【掌上營(yíng)業(yè)廳】+888888888888"; ?
? ? ? ? ? ? ? ? ? ? matcher.appendReplacement(strbuff, Matcher.quoteReplacement(se == null ? "" : se)); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? i++; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? matcher.appendTail(strbuff); ?
? ? ? ? htmlStr += strbuff.toString(); ?
? ? ? ? System.out.println(htmlStr + "=================" + i); ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 方法描述:判斷傳入的字符串是否非空,即:字符串是否等于null、""或" "。?
? ? ?* 創(chuàng)建日期:2013-12-7下午11:39:34?
? ? ?* 修改日期:?
? ? ?* 作者:zhanglu?
? ? ?* @param:?
? ? ?* @return:boolean?
? ? ?*/ ?
? ? public static boolean isNotEmpty(String str) ?
? ? { ?
? ? ? ? if ((null == str) || ("".equals(str.trim()))) ?
? ? ? ? { ?
? ? ? ? ? ? return false; ?
? ? ? ? } ?
? ? ? ? else ?
? ? ? ? { ?
? ? ? ? ? ? return true; ?
? ? ? ? } ?
? ? } ?
? ? ??
? ? /**?
? ? ?* 方法描述:將傳入的字符串轉(zhuǎn)換成整型數(shù)據(jù),如果轉(zhuǎn)換過(guò)程中發(fā)生異常,則返回默認(rèn)值:defaultValue。?
? ? ?* 創(chuàng)建日期:2013-12-7下午11:40:20?
? ? ?* 修改日期:?
? ? ?* 作者:zhanglu?
? ? ?* @param:?
? ? ?* @return:int?
? ? ?*/ ?
? ? public static int convertIntoInt(String str, int defaultValue) ?
? ? { ?
? ? ? ? // 定義一個(gè)返回值,假如轉(zhuǎn)型過(guò)程中發(fā)生異常,則返回此默認(rèn)值 ?
? ? ? ? int retData = defaultValue; ?
? ? ? ? try ?
? ? ? ? { ?
? ? ? ? ? ? retData = Integer.parseInt(str.trim()); ?
? ? ? ? } ?
? ? ? ? catch(NumberFormatException e) ?
? ? ? ? { ?
? ? ? ? } ?
? ? ? ? catch(Exception ex) ?
? ? ? ? { ?
? ? ? ? } ?
??
? ? ? ? return retData; ?
? ? } ?
??
}
總結(jié)
以上是生活随笔為你收集整理的StringUtil详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vue.js:利用vue.js做一个抽奖
- 下一篇: [MM9]复制格式的快捷操作