1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453 |
- package com.yihu.jw.utils;
- import org.apache.commons.lang3.StringUtils;
- import javax.servlet.http.HttpServletRequest;
- import java.io.IOException;
- import java.io.StringWriter;
- import java.io.UnsupportedEncodingException;
- import java.io.Writer;
- import java.sql.Clob;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class StringUtil {
- public static String substring(String str, int start) {
- return StringUtils.substring(str, start);
- }
- public static String substring(String str, int start, int end) {
- return StringUtils.substring(str, start, end);
- }
- public static boolean isBlank(String str) {
- return StringUtils.isBlank(str);
- }
- public static String toString(Object obj) {
- if (obj == null) {
- return null;
- }
- return obj.toString();
- }
- public static String trimLeft(String value) {
- if (value == null)
- return "";
- String result = value;
- char ch[] = result.toCharArray();
- int index = -1;
- for (int i = 0; i < ch.length; i++) {
- if (Character.isWhitespace(ch[i])) {
- index = i;
- } else {
- break;
- }
- }
- if (index != -1) {
- result = result.substring(index + 1);
- }
- return result;
- }
- /**
- * ?��??????????????????
- *
- * @param value ?????
- * @return String src????????
- */
- public static String trimRight(String value) {
- if (value == null)
- return "";
- String result = value;
- char ch[] = result.toCharArray();
- int endIndex = -1;
- for (int i = ch.length - 1; i > -1; i--) {
- if (Character.isWhitespace(ch[i])) {
- endIndex = i;
- } else {
- break;
- }
- }
- if (endIndex != -1) {
- result = result.substring(0, endIndex);
- }
- return result;
- }
- /**
- * ***************************************************** ??
- * ??????????????????"0",?????????? ????????param str:???????????? param len???????????? ?? ???Stirng
- * <p/> ??��????? ??????? ******************************************************
- */
- public static String fillHeadCharsLen(String strOri, int len) {
- return fillHeadCharsLen(strOri, "0", len);
- }
- /**
- * ***************************************************** ??
- * ???????????????????"0",?????????? ????????param str:???????????? param len???????????? ?? ???Stirng
- * <p/> ??��????? ??????? ******************************************************
- */
- public static String fillBackCharsLen(String strOri, int len) {
- return fillBackCharsLen(strOri, "0", len);
- }
- /**
- * ***************************************************** ??
- * ??????????????????????????,?????????? ????????param str:???????????? param subStr:????????????
- * param len???????????? ?? ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillHeadCharsLen(String strOri, String subStr, int len) {
- if (strOri == null || strOri.trim().length() == 0) {
- strOri = "";
- }
- if (subStr == null) {
- subStr = " ";
- }
- String fillStr = "";
- for (int i = 0; i < len; i++) {
- fillStr = fillStr + subStr;
- }
- subStr = fillStr + strOri;
- return (subStr.substring(subStr.length() - len, subStr.length()));
- }
- /**
- * ***************************************************** ??
- * ???????????????????????????,?????????? ????????param str:???????????? param subStr:????????????
- * param len???????????? ?? ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillBackCharsLen(String strOri, String subStr, int len) {
- if (strOri == null || strOri.trim().length() == 0) {
- strOri = "";
- }
- if (subStr == null) {
- subStr = " ";
- }
- String fillStr = "";
- for (int i = 0; i < len; i++) {
- fillStr = fillStr + subStr;
- }
- subStr = strOri + fillStr;
- return (subStr.substring(0, len));
- }
- /**
- * ***************************************************** ?? ??????????????????"0"
- * ????????param str:???????????? param counter??????????? ?? ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillHeadChars(String strOri, int counter) {
- return fillHeadChars(strOri, "0", counter);
- }
- /**
- * ***************************************************** ?? ???????????????????"0"
- * ????????param str:???????????? param counter??????????? ?? ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillBackChars(String strOri, int counter) {
- return fillBackChars(strOri, "0", counter);
- }
- /**
- * ***************************************************** ?? ??????????????????????????
- * ????????param str:???????????? param subStr:???????????? param counter??????????? ??
- * ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillHeadChars(String strOri, String subStr, int counter) {
- if (strOri == null || strOri.trim().length() == 0) {
- strOri = "";
- }
- if (counter <= 0 || subStr == null) {
- return strOri;
- }
- String fillStr = "";
- for (int i = 0; i < counter; i++) {
- fillStr = fillStr + subStr;
- }
- return (fillStr + strOri);
- }
- /**
- * ***************************************************** ?? ???????????????????????????
- * ????????param str:???????????? param subStr:???????????? param counter??????????? ??
- * ???Stirng <p/> ??��????? ???????
- * ******************************************************
- */
- public static String fillBackChars(String strOri, String subStr, int counter) {
- if (strOri == null || strOri.trim().length() == 0) {
- strOri = "";
- }
- if (counter <= 0 || subStr == null) {
- return strOri;
- }
- String fillStr = "";
- for (int i = 0; i < counter; i++) {
- fillStr = fillStr + subStr;
- }
- return (strOri + fillStr);
- }
- /**
- * ***************************************************** ??
- * ????��???????????????null(???????) ????????param str:???????????? ????????? ?? ??????null??true;
- * ?????false <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isEmpty(Object strObj) {
- if (strObj == null || strObj.toString().trim().length() < 1) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * ***************************************************** ??
- * ????��??????????????????null????(???????)?? ????????param str:???��??????? ????????? ??
- * ???true?????false????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isStrEmpty(String str) {
- if ((str == null) || (str.trim().length() < 1) || "null".endsWith(str.toLowerCase())) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * ***************************************************** ??
- * ????????????????????????????????null????? ????????param str:???????????? ????????? ??
- * ???????????????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String getValue(String str) {
- if (str == null) {
- return "";
- }
- if (str.trim().length() <= 0)
- return "";
- str = "H" + str;
- str = str.trim();
- str = str.substring(1);
- return str;
- }
- /**
- * ***************************************************** ?? ????��?????????????????????
- * ????????param text:???��?????????param len:????????? ????????? ?? ???return
- * true??��??????false??????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean chkTextLen(String text, int len) {
- if (text == null || text.length() > len) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * ***************************************************** ??
- * ????��??????????????????????????? ????????param text:???��?????????param len:????????? ????????? ??
- * ???return true??��??????false??????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean chkTextTrimLen(String text, int len) {
- if (text == null || text.trim().length() > len) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * ***************************************************** ??
- * ????��???????????????english ????????param text:???��????????? ????????? ?? ???return
- * true???????false:????????????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isStrEn(String text) {
- for (int i = 0; i < text.length(); i++) {
- if (text.charAt(i) > 127) {
- return false;
- }
- }
- return true;
- }
- /**
- * ***************************************************** ?? ????��????????????????
- * ????????param ch:???��????? ????????? ?? ???return true???????false:?????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isCharNum(char ch) {
- if (ch > 47 && ch < 58) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * ***************************************************** ??
- * ????��?????????????��????????? ????????param str:???��??????? ????????? ?? ???return
- * true??Null?????????????false:??????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isStrNum(String str) {
- if (isStrEmpty(str)) {
- return true;
- }
- boolean notNum = false;
- for (int i = 0; i < str.length(); i++) {
- if (!isCharNum(str.charAt(i))) {
- notNum = true;
- }
- }
- return !notNum;
- }
- /**
- * ***************************************************** ??
- * ????��?????????????��????????? ????????param strSrc:???��??????? ????????? ?? ???return
- * true??????????false:????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isNum(String strSrc) throws Exception {
- for (int i = 0; i < strSrc.length(); i++) {
- if (!isCharNum(strSrc.charAt(i)))
- return false;
- }
- return true;
- }
- /**
- * ***************************************************** ?? ????��??????????????????
- * ????????param ch:???��????? ????????? ?? ???return true????????????false:?????????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isCharLetter(char ch) {
- if ((ch >= 65 && ch <= 90) && (ch >= 97 && ch <= 122)) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * ***************************************************** ?? ????��?????????????????????
- * ????????param str:???��??????? ????????? ?? ???return true??NULL???��?????????false:?????????
- * <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean isStrLetter(String str) {
- if (isStrEmpty(str))
- return true;
- boolean notLetter = false;
- for (int i = 0; i < str.length(); i++) {
- if (!isCharLetter(str.charAt(i))) {
- notLetter = true;
- }
- }
- return !notLetter;
- }
- /**
- * ***************************************************** ??
- * ?????????????????????????????? ????????param src:?????????? ????????? ?? ???return
- * ??????????????????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static char strToChar(String src) {
- src = src.trim();
- char result = src.charAt(0);
- return result;
- }
- /**
- * ***************************************************** ??
- * ??????????????????????ASCII?? ????????param sql:???????sql?????? ????????? ?? ?????????????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static String encodeSQL(String sql) {
- StringBuffer tempBuff = new StringBuffer();
- for (int i = 0; i < sql.length(); i++) {
- tempBuff.append(Integer.toHexString(sql.charAt(i)));
- }
- return tempBuff.toString();
- }
- /**
- * ***************************************************** ??
- * ??????????ASCII????????????????? ????????param encoded:???????????? ????????? ?? ?????????????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static String decodeSQL(String encoded) {
- StringBuffer tempBuff = new StringBuffer();
- for (int i = 0; i < encoded.length(); i += 2) {
- tempBuff.append((char) Integer.parseInt(
- encoded.substring(i, i + 2), 16));
- }
- return tempBuff.toString();
- }
- /**
- * ***************************************************** ?? ?????????��??
- * ????????param path1:????��????param context1:?????? ????????? ?? ???return ???��?? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static String getAbsolutePath(String path1, String context1) {
- int i1 = path1.indexOf(context1);
- if (i1 < 0) {
- return path1;
- } else {
- return path1.substring(path1.indexOf(context1) + context1.length());
- }
- }
- /**
- * ***************************************************** ?? ????????? ????????param
- * str1:???????????????param sindex:???��???param eindex:????��?? ????????? ??
- * ???????????��????????��???????????????????��??��??0?????????��????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String getSubString(String str1, int sindex, int eindex) {
- if (str1 == null) {
- return "";
- }
- if (str1.trim().length() <= 0)
- return "";
- if (str1.length() > sindex) {
- if (eindex >= 0)
- return str1.substring(sindex, eindex);
- else if (eindex < 0)
- return str1.substring(sindex);
- }
- return "";
- }
- /**
- * ***************************************************** ??
- * ????????????????????????��??????????????? ????????param strs:???????????????�param size1:?????????�A??
- * ????????? ?? ???return ??????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[] getValues(String[] strs, int size1) {
- String[] strs1 = new String[size1];
- for (int i = 0; i < size1; i++) {
- strs1[i] = "";
- }
- if (strs == null) {
- return strs1;
- } else {
- if (strs.length < size1) {
- for (int i = 0; i < strs.length; i++) {
- strs1[i] = strs[i];
- }
- return strs1;
- } else {
- return strs;
- }
- }
- }
- /**
- * ***************************************************** ?? ????????????�I????
- * ????????param strSource ???�I?????????param strFrom ????????param strTo ???????? ????????? ??
- * ????�I???????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String replaceStrAll(String strSource, String strFrom,
- String strTo) {
- String strDest = "";
- int intFromLen = strFrom.length();
- int intPos;
- while ((intPos = strSource.indexOf(strFrom)) != -1) {
- strDest = strDest + strSource.substring(0, intPos);
- strDest = strDest + strTo;
- strSource = strSource.substring(intPos + intFromLen);
- }
- strDest = strDest + strSource;
- return strDest;
- }
- public static String replaceStr(String strTarget, String strNew) {
- int iIndex = -1;
- while (true) {
- iIndex = strTarget.indexOf('\n');
- if (iIndex < 0) {
- break;
- }
- String strTemp = null;
- strTemp = strTarget.substring(0, iIndex);
- strTarget = strTemp + strNew + strTarget.substring(iIndex + 1);
- }
- return strTarget;
- }
- /**
- * ***************************************************** ??
- * ????��????????????????��?????? ????????param str1 ?????????????param strarray ??????????????�
- * ????????? ?? ???return true??????;false??��???? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static boolean includestr(String str1, String[] strarray) {
- if (strarray == null || strarray.length <= 0)
- return false;
- for (int i = 0; i < strarray.length; i++) {
- if (strarray[i] == null) {
- if (str1 == null)
- return true;
- else
- continue;
- }
- if (strarray[i].trim().equals(str1)) {
- return true;
- }
- }
- return false;
- }
- /**
- * ***************************************************** ??
- * ?????\n?????????????????????????�?????\r ????????param fvalue ???????????? ????????? ?? ???return
- * ?????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[] getAreaValues(String fvalue) {
- String tmpstr = fvalue;
- int i = 0;
- if (tmpstr == null)
- return null;
- if (tmpstr.trim().equals(""))
- return null;
- while (tmpstr.indexOf("\n") >= 0) {
- i++;
- tmpstr = tmpstr.substring(tmpstr.indexOf("\n") + 1);
- }
- if (tmpstr.trim().equals("")) {
- i--;
- }
- String[] fvalues = new String[i + 1];
- tmpstr = fvalue;
- i = 0;
- while (tmpstr.indexOf("\n") >= 0) {
- fvalues[i] = tmpstr.substring(0, tmpstr.indexOf("\n"));
- if (fvalues[i].indexOf("\r") >= 0)
- fvalues[i] = fvalues[i].substring(0, fvalues[i].indexOf("\r"));
- i++;
- tmpstr = tmpstr.substring(tmpstr.indexOf("\n") + 1);
- }
- if (!tmpstr.trim().equals(""))
- fvalues[i] = tmpstr;
- return fvalues;
- }
- /**
- * ***************************************************** ?? ???????????��?|????\n
- * ????????param fvalue ???????????? ????????? ?? ?????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String getrealAreaValues(String fvalue) {
- String tmpstr = fvalue;
- String returnstr = "";
- if (tmpstr == null)
- return null;
- if (tmpstr.trim().equals(""))
- return "";
- while (tmpstr.indexOf("|") > 0) {
- returnstr += tmpstr.substring(0, tmpstr.indexOf("|")) + "\n";
- tmpstr = tmpstr.substring(tmpstr.indexOf("|") + 1);
- }
- return returnstr;
- }
- /**
- * ***************************************************** ??
- * ?????????????��??��???????????? ????????param strInput ??????????????param chr ????????? ????????? ??
- * ???return ???????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static int countChar(String strInput, char chr) {
- int iCount = 0;
- char chrTmp = ' ';
- if (strInput.trim().length() == 0)
- return 0;
- // ?????????????????
- for (int i = 0; i < strInput.length(); i++) {
- chrTmp = strInput.charAt(i);
- if (chrTmp == chr) {
- iCount++;
- }
- }
- return iCount;
- }
- /**
- * ***************************************************** ?? ??????????????????????????
- * ????????param strs ???????????????? ????????? ?? ????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String strArrayToStr(String[] strs) {
- return strArrayToStr(strs, null);
- }
- /**
- * ***************************************************** ?? ??????��???????????????????
- * ????????param strs ???????????????? ????????? ?? ????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static void printStrs(String[] strs) {
- for (int i = 0; i < strs.length; i++) {
- System.out.println(strs[i]);
- }
- }
- /**
- * ***************************************************** ??
- * ???????????��??????��??????????????????? ????????param strs ?????????????????? ????????? ?? ????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static void printDualStr(String[][] dualStr) {
- for (int i = 0; i < dualStr.length; i++) {
- for (int j = 0; j < dualStr[i].length; j++) {
- System.out.print(dualStr[i][j] + " ");
- }
- System.out.println();
- }
- }
- /**
- * ***************************************************** ??
- * ???????????????????????��???��????��???? ????????param dualStr ??????????? ????????? ?? ???return
- * ???????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[][] rowToColumn(String[][] dualStr) {
- String[][] returnDualStr = null;
- if (dualStr != null) {
- returnDualStr = new String[dualStr[0].length][dualStr.length];
- for (int i = 0; i < dualStr.length; i++)
- for (int j = 0; j < dualStr[0].length; j++)
- returnDualStr[j][i] = dualStr[i][j];
- }
- return returnDualStr;
- }
- /**
- * ***************************************************** ??
- * ????????????????????String?????��???????????????? ????????param inStr ?????????? ????????? ?? ???return
- * ??????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String latinString(String inStr) {
- String res = inStr;
- if (null == res)
- return null;
- res = replaceStrAll(res, "\"", "\\\"");
- res = replaceStrAll(res, "'", "\\'");
- return res;
- }
- /**
- * ***************************************************** ?? ?????????????��???�I???????
- * ????????param String strTarget, String strNew ????????? ?? ???return String
- * ????????�I????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String replaceWhiteSpace(String strTarget, String strNew) {
- int iIndex = -1;
- while (true) {
- char cRep = 32;
- iIndex = strTarget.indexOf(cRep);
- if (iIndex < 0) {
- break;
- }
- String strTemp = null;
- strTemp = strTarget.substring(0, iIndex);
- strTarget = strTemp + strNew + strTarget.substring(iIndex + 1);
- }
- return strTarget;
- }
- /**
- * ***************************************************** ??
- * ?????????��?????????��????��?????????????��????��?????????��???? ????????param amount ???????, param
- * length ?????��??��???? ????????? ?? ???return ??????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String double2str(double amount, int length) {
- String strAmt = Double.toString(amount);
- int pos = strAmt.indexOf('.');
- if (pos != -1 && strAmt.length() > length + pos + 1)
- strAmt = strAmt.substring(0, pos + length + 1);
- return strAmt;
- }
- /**
- * ***************************************************** ??
- * ???????chr?????????????String???????split???????"|"??????? ????????param str ??????????,param
- * chr ?????? ????????? ?? ???return String[] ??????????????,????????????|?????????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[] doSplit(String str, char chr) {
- int iCount = 0;
- char chrTmp = ' ';
- // ?????????????????
- for (int i = 0; i < str.length(); i++) {
- chrTmp = str.charAt(i);
- if (chrTmp == chr) {
- iCount++;
- }
- }
- String[] strArray = new String[iCount];
- for (int i = 0; i < iCount; i++) {
- int iPos = str.indexOf(chr);
- if (iPos == 0) {
- strArray[i] = "";
- } else {
- strArray[i] = str.substring(0, iPos);
- }
- str = str.substring(iPos + 1); // ??iPos+1??????,str????????��
- }
- return strArray;
- }
- /**
- * ***************************************************** ??
- * ???????s?????????????String???????split???????"|"??????? ????????param str ??????????,param s
- * ???????? ????????? ?? ???return String[] ??????????????,??????????|?????????? <p/>
- * ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[] strSplit(String src, String splitchar) {
- int resultSize = 0;
- int len = src.length();
- int idx = 0;
- String strTemp = "";
- for (int i = 0; i < len; i++) {
- if (src.substring(i, i + 1).equals(splitchar)) {
- resultSize++;
- }
- }
- if ((len > 1) & (!src.substring(len - 1, len).equals(splitchar))) {
- resultSize++;
- }
- String result[] = new String[resultSize];
- for (int i = 0; i < len; i++) {
- if (src.substring(i, i + 1).equals(splitchar)) {
- result[idx] = strTemp;
- idx++;
- strTemp = "";
- } else {
- strTemp = String.valueOf(strTemp)
- + String.valueOf(src.charAt(i));
- }
- }
- if (!strTemp.equals("")) {
- result[idx] = strTemp;
- }
- return result;
- }
- /**
- * ***************************************************** ??
- * ???????strSeparator?????????????????????��??????? ????????param strToSplit ??????????,param
- * strSeparator ????????,param iLimit ?????�� ????????? ?? ???return String[] ??????????????
- * <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String[] split(String strToSplit, String strSeparator,
- int iLimit) {
- ArrayList tmpList = new ArrayList();
- int iFromIndex = 0;
- int iCurIndex = strToSplit.length();
- String strUnitInfo = "";
- int iCurCounts = 0;
- while ((iCurIndex != -1) && (iFromIndex < strToSplit.length())
- && (iCurCounts < iLimit)) {
- iCurIndex = strToSplit.indexOf(strSeparator, iFromIndex);
- if (iCurIndex == -1) {
- strUnitInfo = strToSplit.substring(iFromIndex, strToSplit
- .length());
- } else {
- strUnitInfo = strToSplit.substring(iFromIndex, iCurIndex);
- iFromIndex = iCurIndex + 1;
- }
- tmpList.add(strUnitInfo);
- iCurCounts++;
- }
- int iCounts = tmpList.size();
- String tmpArray[] = new String[iCounts];
- for (int i = 0; i < iCounts; i++) {
- tmpArray[i] = (String) tmpList.get(i);
- }
- return tmpArray;
- }
- /**
- * ***************************************************** ??
- * ????????????��????????????????????...???? ????????param src ???????????,param len ?????�� ????????? ??
- * ???return String ??��????????????????????...??????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String strIntercept(String src, int len) {
- if (src == null) {
- return "";
- }
- if (src.length() > len) {
- src = String.valueOf(String.valueOf(src.substring(0, len))).concat(
- "...");
- }
- return src;
- }
- /**
- * ***************************************************** ??
- * ?????????????ISO8859_1???? ????????param str_in ??????????????? ????????? ?? ???return String
- * ????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public static String strtochn(String str_in) {
- try {
- String temp_p = str_in;
- if (temp_p == null) {
- temp_p = "";
- }
- String temp = "";
- if (!temp_p.equals("")) {
- byte[] byte1 = temp_p.getBytes("ISO8859_1");
- temp = new String(byte1);
- }
- return temp;
- } catch (Exception e) {
- }
- return "null";
- }
- /**
- * ***************************************************** ??
- * ?????ISO8859_1????????GBK???? ????????param strvalue ??????????????? ????????? ?? ???return
- * String ????????????? ???????
- * ******************************************************
- */
- public static String ISO2GBK(String strvalue) {
- try {
- if (strvalue == null)
- return null;
- else {
- strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
- return strvalue;
- }
- } catch (Exception e) {
- return null;
- }
- }
- /**
- * ***************************************************** ??
- * ?????????????????????��?????? ????????param str ??????????????? ????????? ?? ???return String
- * ????????????? ??????? ******************************************************
- */
- public static String cnCodeTrans(String str) {
- String s = "";
- try {
- s = new String(str.getBytes("GB2312"), "8859_1");
- } catch (UnsupportedEncodingException a) {
- System.out.print("chinese thansform exception");
- }
- return s;
- }
- /**
- * ***************************************************** ??
- * ????��?????????????????STaaaa????ST****** ????????param strSource???????????????param
- * strRule???? ????????? ?? ???return false:???????????true:????????? ???????
- * ******************************************************
- */
- public static boolean judgeMatch(String strSource, String strRule) {
- int i = 0;
- // ????????��?
- if ((null == strSource) || (strSource.length() == 0))
- return false;
- // ????????��?
- if ((null == strRule) || (strRule.length() == 0))
- return false;
- // ??????????
- if (strSource.length() > strRule.length())
- return false;
- // ??��???��?
- for (i = 0; i < strRule.length(); i++) {
- // ?????????
- if (strSource.length() < i + 1) {
- break;
- }
- if ((strRule.charAt(i) != '*')
- && (strSource.charAt(i) != strRule.charAt(i))) {
- return false;
- }
- }
- // ??????????????????????????????'*'?????????
- for (; i < strRule.length(); i++) {
- if (strRule.charAt(i) != '*')
- return false;
- }
- return true;
- }
- public static String column2Property(String column) {
- column = column.toLowerCase();
- int i = column.indexOf("_");
- while (i != -1) {
- if (i != column.length() - 1) {
- char temp = column.charAt(i + 1);
- String strTemp = String.valueOf(temp);
- column = column.replaceFirst("_" + strTemp, strTemp
- .toUpperCase());
- i = column.indexOf("_");
- } else {
- break;
- }
- }
- return column;
- }
- public static String strArrayToStr(String[] strs, String separator) {
- StringBuffer returnstr = new StringBuffer("");
- if (strs == null)
- return "";
- if (separator == null)
- separator = "";
- for (int i = 0; i < strs.length; i++) {
- returnstr.append(strs[i]);
- if (i < strs.length - 1)
- returnstr.append(separator);
- }
- return returnstr.toString();
- }
- public static String objectArrayToStr(Object[] objects, String separator) {
- StringBuffer returnstr = new StringBuffer("");
- if (objects == null)
- return "";
- if (separator == null)
- separator = "";
- for (int i = 0; i < objects.length; i++) {
- returnstr.append(String.valueOf(objects[i]));
- if (i < objects.length - 1)
- returnstr.append(separator);
- }
- return returnstr.toString();
- }
- public static String listToStr(List element, String separator) {
- StringBuffer returnstr = new StringBuffer("");
- if (element == null)
- return "";
- if (separator == null)
- separator = "";
- Iterator it = element.iterator();
- while (it.hasNext()) {
- returnstr.append(String.valueOf(it.next()));
- if (it.hasNext())
- returnstr.append(separator);
- }
- return returnstr.toString();
- }
- public static String[] listToStrArray(List element) {
- if (element == null || element.size() == 0)
- return null;
- Iterator it = element.iterator();
- String[] strArray = new String[element.size()];
- int i = 0;
- while (it.hasNext()) {
- strArray[i] = String.valueOf(it.next());
- i++;
- }
- return strArray;
- }
- public static List strToList(String str, String separator) {
- if (str == null || str.equals(""))
- return null;
- if (separator == null)
- separator = "";
- String[] strArr = str.split(separator);
- int size = strArr.length;
- List list = new ArrayList();
- for (int i = 0; i < size; i++) {
- list.add(strArr[i]);
- }
- return list;
- }
- public static StringBuffer populate(StringBuffer bf, String value,
- boolean isNotLast) {
- if (value == null) {
- return bf;
- }
- // ???????????????????�I??????????????????????????????SQL???????????
- System.out.println(value.replaceAll("'", "''"));
- bf.append("'").append(value.replaceAll("'", "''")).append("'");
- if (isNotLast)
- bf.append(",");
- return bf;
- }
- public static boolean isExist(String str, String substr, String sepatator) {
- if (str == null || str.trim().equals(""))
- return false;
- if (substr == null || substr.trim().equals(""))
- return false;
- String[] strArr = str.split(sepatator);
- int size = strArr.length;
- for (int i = 0; i < size; i++) {
- if (strArr[i].equals(substr))
- return true;
- }
- return false;
- }
- public static boolean isExist(String str, String substr) {
- return isExist(str, substr, ",");
- }
- public static String leftInclude(String str) {
- if (str == null || str.equals(""))
- return str;
- return str + "%";
- }
- public static String rightInclude(String str) {
- if (str == null || str.equals(""))
- return str;
- return "%" + str;
- }
- public static String include(String str) {
- if (str == null || str.equals(""))
- return str;
- return "%" + str + "%";
- }
- /**
- * ???????????????????????????????
- *
- * @param source String ?????
- * @param target String ????????
- * @return String ???
- */
- public static String nvl(Object source, Object target) {
- return source != null ? String.valueOf(source)
- : (target != null ? String.valueOf(target) : null);
- }
- /**
- * ??clob??????????????
- *
- * @param clobValue
- * @return
- * @throws SQLException
- */
- public static String clob2Str(Object clobValue) throws SQLException {
- return ((Clob) clobValue).getSubString(1, (int) ((Clob) clobValue)
- .length());
- }
- /**
- * ???web????????Ip
- *
- * @param request
- * @return
- */
- public static String getRemoteAddr(HttpServletRequest request) {
- String ip = request.getHeader("x-forwarded-for");
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- return ip;
- }
- public static String hideFlowStr(String str, int maxLen) {
- if (str == null || maxLen == 0) {
- return "";
- }
- if (str.length() > maxLen) {
- str = str.substring(0, maxLen) + "...";
- }
- return str;
- }
- /**
- * ???????????????
- *
- * @param str
- * @return
- */
- public static String escapeLuceneSpecialCharacters(String str) {
- if (str == null) {
- return null;
- }
- try {
- StringWriter writer = new StringWriter(str.length() * 2);
- escapeLuceneSpecialCharacterString(writer, str);
- return writer.toString();
- } catch (IOException ex) {
- //LogService.getLogger(StringUtil.class).error(ex.getMessage());
- return null;
- }
- }
- private static void escapeLuceneSpecialCharacterString(Writer out,
- String str) throws IOException {
- if (str == null) {
- return;
- }
- int sz = str.length();
- for (int i = 0; i < sz; i++) {
- char ch = str.charAt(i);
- /*
- * Reference link:
- * http://lucene.apache.org/java/docs/queryparsersyntax.html Lucene
- * supports escaping special characters that are part of the query
- * syntax. The current list special characters are: + - && || ! ( ) { } [ ] ^ " ~ * ? : \
- * To escape these character use the \ before the character.
- */
- switch (ch) {
- case '+':
- case '-':
- case '!':
- case '(':
- case ')':
- case '{':
- case '}':
- case '[':
- case ']':
- case '^':
- case '\"':
- case '~':
- case '*':
- case '?':
- case ':':
- case '\\':
- out.write('\\');
- out.write(ch);
- break;
- case '&':
- case '|':
- // check if it is '&&' or '||' ~ check if the next char is '&'
- // or '|'
- if ((i + 1) < sz && str.charAt(i + 1) == ch) {
- out.write('\\');
- out.write(ch);
- i++;
- }
- out.write(ch);
- break;
- default:
- out.write(ch);
- break;
- }
- }
- }
- /**
- * ???????????????????????
- */
- public static String getFirstLetter(String chinese) {
- /**
- * ???
- */
- final int[] li_SecPosValue = {1601, 1637, 1833, 2078, 2274, 2302,
- 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
- 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590};
- final String[] lc_FirstLetter = {"A", "B", "C", "D", "E", "F", "G",
- "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
- "W", "X", "Y", "Z"};
- if (chinese == null || chinese.trim().length() == 0) {
- return "";
- }
- chinese = conversionStr(chinese, "GB2312", "ISO8859-1");
- if (chinese.length() > 1) { // ?��?????????
- int li_SectorCode = (int) chinese.charAt(0); // ????????
- int li_PositionCode = (int) chinese.charAt(1); // ????��??
- li_SectorCode = li_SectorCode - 160;
- li_PositionCode = li_PositionCode - 160;
- int li_SecPosCode = li_SectorCode * 100 + li_PositionCode;// ??????��??
- if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
- for (int i = 0; i < 23; i++) {
- if (li_SecPosCode >= li_SecPosValue[i]
- && li_SecPosCode < li_SecPosValue[i + 1]) {
- chinese = lc_FirstLetter[i];
- break;
- }
- }
- } else {
- chinese = conversionStr(chinese, "ISO8859-1", "GB2312");
- chinese = chinese.substring(0, 1);
- }
- }
- return chinese;
- }
- private static String conversionStr(String str, String charsetName,
- String toCharsetName) {
- try {
- str = new String(str.getBytes(charsetName), toCharsetName);
- } catch (UnsupportedEncodingException ex) {
- System.out.println("????????????????" + ex.getMessage());
- }
- return str;
- }
- /**
- * ????????????
- *
- * @param html
- * @return
- */
- public static String checkStr(String html) {
- try {
- html = html.replaceAll("\r", "");
- html = html.replaceAll("\n", "");
- html = html.replaceAll("\"", "'");
- html = html.replaceAll("\t", " ");
- Pattern p_script;
- Matcher m_script;
- Pattern p_href;
- Matcher m_href;
- Pattern p_a;
- Matcher m_a;
- Pattern p_on;
- Matcher m_on;
- Pattern p_iframe;
- Matcher m_iframe;
- Pattern p_frameset;
- Matcher m_frameset;
- Pattern p_img;
- Matcher m_img;
- Pattern p_p1;
- Matcher m_p1;
- Pattern p_p2;
- Matcher m_p2;
- String stript_str = "<script[\\s\\S]+</script *>";
- String href_str = " href *= *[\\s\\S]*script *:";
- String on_str = " on[\\s\\S]*=";
- String iframe_str = "<iframe[\\s\\S]+</iframe *>";
- String frameset_str = "<frameset[\\s\\S]+</frameset *>";
- String a_str = "<a ([^>])*>.*?</a([^>])*>";
- //String img_str = "\\<img[^\\>]+\\>";
- String p_str1 = "</p>";
- String p_str2 = "<p>";
- p_script = Pattern.compile(stript_str, Pattern.CASE_INSENSITIVE);
- m_script = p_script.matcher(html);
- html = m_script.replaceAll(""); // ????script???
- p_href = Pattern.compile(href_str, Pattern.CASE_INSENSITIVE);
- m_href = p_href.matcher(html);
- html = m_href.replaceAll(""); // ????script???
- p_a = Pattern.compile(a_str, Pattern.CASE_INSENSITIVE);
- m_a = p_a.matcher(html);
- html = m_a.replaceAll(""); // ????script???
- p_on = Pattern.compile(on_str, Pattern.CASE_INSENSITIVE);
- m_on = p_on.matcher(html);
- html = m_on.replaceAll(""); // ????script???
- p_iframe = Pattern.compile(iframe_str, Pattern.CASE_INSENSITIVE);
- m_iframe = p_iframe.matcher(html);
- html = m_iframe.replaceAll(""); // ????script???
- p_frameset = Pattern
- .compile(frameset_str, Pattern.CASE_INSENSITIVE);
- m_frameset = p_frameset.matcher(html);
- html = m_frameset.replaceAll(""); // ????script???
- // p_img = Pattern.compile(img_str, Pattern.CASE_INSENSITIVE);
- // m_img = p_img.matcher(html);
- // html = m_img.replaceAll(""); // ????script???
- p_p1 = Pattern.compile(p_str1, Pattern.CASE_INSENSITIVE);
- m_p1 = p_p1.matcher(html);
- html = m_p1.replaceAll(""); // ????script???
- p_p2 = Pattern.compile(p_str2, Pattern.CASE_INSENSITIVE);
- m_p2 = p_p2.matcher(html);
- html = m_p2.replaceAll(""); // ????script???
- } catch (Exception e) {
- System.err.println("Html2Text: " + e.getMessage());
- }
- return html;
- }
- public static String substr(String value, int maxlength, String postfix) {
- int k = 0;
- int l = 0;
- for (int i = 0; i < value.length() && maxlength > l * 2 + k; i++) {
- if (value.charAt(i) > '\200') {
- l++;
- } else {
- k++;
- }
- }
- // value????????????????????
- if (l + k >= value.length()) {
- return value;
- } else if (maxlength >= l * 2 + k && l + k > 0) {
- value = value.substring(0, l + k);
- } else if (l + k > 0) {
- value = value.substring(0, (l + k) - 1);
- } else {
- return value;
- }
- // ????????????????????????????????????????
- if (!StringUtils.isEmpty(postfix)) {
- value += postfix;
- }
- return value;
- }
- /**
- * ***************************************************** ??
- * ??????��??????????null???????????????????? ????????param Content:???????????? ????????? ??
- * ?????????????null???????????????????? <p/> ??��?????20051220 ???????
- * ******************************************************
- */
- public String nullToSpace(String Content) {
- if (Content == null) {
- Content = "";
- }
- return Content;
- }
- /**
- * ***************************************************** ??
- * ?????GBK????????ISO8859_1???? ????????param strvalue ??????????????? ????????? ?? ???return
- * String ????????????? ???????
- * ******************************************************
- */
- public String GBK2ISO(String strvalue) throws Exception {
- try {
- if (strvalue == null)
- return null;
- else {
- strvalue = new String(strvalue.getBytes("GBK"), "ISO8859_1");
- return strvalue;
- }
- } catch (Exception e) {
- return null;
- }
- }
- /**
- * 去除换行符
- * @param str
- * @return
- */
- public static String replaceBlank(String str) {
- String dest = "";
- if (str!=null) {
- Pattern p = Pattern.compile("\\s*|\t|\r|\n");
- Matcher m = p.matcher(str);
- dest = m.replaceAll("");
- }
- return dest;
- }
- /**
- * 生成随机
- * @return
- */
- private String getRandomIntStr(){
- Random rand = new Random();
- int i = rand.nextInt(); //int范围类的随机数
- i = rand.nextInt(100); //生成0-100以内的随机数
- i = (int)(Math.random() * 100000000); //0-100以内的随机数,用Matn.random()方式
- return String.valueOf(i);
- }
- }
|