import java.io.*; import java.text.DecimalFormat; import org.apache.oro.text.regex.PatternCompiler; import org.apache.oro.text.regex.PatternMatcher; import org.apache.oro.text.regex.Perl5Matcher; import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.Perl5Compiler; import org.apache.commons.logging.Log; /** * <p>Title: StringUtils</p> * <p>Description: 字符串工具类; 配合JakartaCommons工具类使用;</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: pubinfo</p> * @author chineseren * @version 1.0 */ public class StringUtils { /** * debug log */ private static Log log = Debuger.getDebuger(StringUtils.class); /** * String charSet 转化 * @param str String * @return String */ public static String toChinese(String str) { if(str == null || str.length() == 0) { return ""; } try { return new String(str.getBytes("ISO8859_1"), "GBK"); } catch(UnsupportedEncodingException ex) { return str; } } /** * 判断字符串是否null或者"" * @param src String * @return boolean */ public static boolean isBlank(String src) { if(null == src || "".equals(src.trim())) { return true; } return false; } /** * @param string - 待判断的字符串是否为null或者空字符串 * @return boolean * @roseuid 3FBE26DE0326 */ public static boolean isNullOrEmpty(String string) { if(string == null || string.trim().equals("")) { return true; } else { return false; } } /** * 把没有实例化的串转化为空串 * @param p_in String * @return String */ public static String convertNull(String str) { if(str == null) { return ""; } else { return str; } } /** * 控制页面显示长度,dataStr为控制长度的字符串,length为字节的长度; * 字符串的字节长度小于显示长度,则原样输出,否则以XXX...的形式输出。 * @param dataStr String * @param length int * @return String */ public static String controlLength(String dataStr, int length) { if(dataStr == null) { return ""; } //转化成格式化的Html dataStr = StringUtils.escape(dataStr); StringBuffer sb = new StringBuffer(); char datach; int lengthi = 0; //字符串的字节长度 int strj = 0; //字符串的实际长度 int lengthi3 = 0; //字节长度-4 //得到字符串的字节长度 while(strj < dataStr.length()) { datach = dataStr.charAt(strj++); if(((datach <= 'z') && (datach >= 'a')) || ((datach <= 'Z') && (datach >= 'A')) || ((datach <= '9') && (datach >= '0'))) { lengthi++; } else { lengthi = lengthi + 2; } } strj = 0; //得到字符串的字节长度-4 while((lengthi3 < length - 4) && (strj < dataStr.length())) { datach = dataStr.charAt(strj++); sb.append(datach); if(((datach <= 'z') && (datach >= 'a')) || ((datach <= 'Z') && (datach >= 'A')) || ((datach <= '9') && (datach >= '0'))) { lengthi3++; } else { lengthi3 = lengthi3 + 2; } } if(lengthi <= length) { return dataStr; } else { sb.append("..."); } return sb.toString(); } /** * 把字符串转化成HTML字符串 * @param str --需要转换的字符串 * @return String --返回把特殊字符转换了的字符串 */ public static String escape(String str) { if(str == null) { return ""; } byte[] data = str.getBytes(); int len = data.length; StringBuffer result = new StringBuffer(len * 2); int begin = 0, count = 0, tt = 0; for(int i = 0; i < len; i++) { switch((char)data[i]) { case '&': result.append(new String(data, begin, count)); result.append("&"); begin = i + 1; count = 0; break; case '\"': result.append(new String(data, begin, count)); result.append("""); begin = i + 1; count = 0; break; case '<': result.append(new String(data, begin, count)); result.append("<"); begin = i + 1; count = 0; break; case '>': result.append(new String(data, begin, count)); result.append(">"); begin = i + 1; count = 0; break; case '\n': result.append(new String(data, begin, count)); result.append("<br>"); begin = i + 1; count = 0; break; case '$': result.append(new String(data, begin, count)); result.append("$$"); begin = i + 1; count = 0; break; case ' ': result.append(new String(data, begin, count)); result.append(" "); begin = i + 1; count = 0; break; default: count++; break; } } if(count > 0) { result.append(new String(data, begin, count)); } return result.toString(); } /** * 把字符串转化成HTML字符串(除了空格) * @param str --需要转换的字符串 * @return String --返回把特殊字符转换了的字符串 */ public static String escape2(String str) { if(str == null) { return ""; } byte[] data = str.getBytes(); int len = data.length; StringBuffer result = new StringBuffer(len * 2); int begin = 0, count = 0, tt = 0; for(int i = 0; i < len; i++) { switch((char)data[i]) { case '&': result.append(new String(data, begin, count)); result.append("&"); begin = i + 1; count = 0; break; case '\"': result.append(new String(data, begin, count)); result.append("""); begin = i + 1; count = 0; break; case '<': result.append(new String(data, begin, count)); result.append("<"); begin = i + 1; count = 0; break; case '>': result.append(new String(data, begin, count)); result.append(">"); begin = i + 1; count = 0; break; case '\n': result.append(new String(data, begin, count)); result.append("<br>"); begin = i + 1; count = 0; break; case '$': result.append(new String(data, begin, count)); result.append("$$"); begin = i + 1; count = 0; break; default: count++; break; } } if(count > 0) { result.append(new String(data, begin, count)); } return result.toString(); } /** * 将textarea输入的文本转化成前台html显示的格式,主要将回车(/r/n)替换成<br>," "替换成 * @param text String * @return String */ public static String textConvertToHtmlText(String text){ if(text != null){ return text.replaceAll("\r\n", "<br>").replaceAll(" ", " "); }else{ return null; } } /** * 全数字判断 * @param strIn String * @return boolean */ public static boolean isNumberString(String strIn) { return isNumberString(strIn, "0123456789"); } /** * 全数字判断,参照字符串strRef可以是:"0123456789","23546"或"0123"等等。 * @param strIn String * @param strRef String * @return boolean */ public static boolean isNumberString(String strIn, String strRef) { if(strIn == null || strIn.length() == 0) return(false); for(int i = 0; i < strIn.length(); i++) { String strTmp = strIn.substring(i, i + 1); if(strRef.indexOf(strTmp, 0) == -1) return(false); } return(true); } /** * 字符串替换,调用了org.apache.commons.lang.StringUtils.replace() * @param strObj 目标字符串 * @param str1 被替换的字符串 * @param str2 替换成的字符串 * @return String */ public static String replace(String strObj, String str1, String str2) { if("".equals(str1)) { return strObj; } return org.apache.commons.lang.StringUtils.replace(strObj, str1, str2); } /** * 根据内容生成摘要 * @param content String * @return String */ public static String createSummary(String content){ if(content == null)return null; String regexp = "<.*?>"; //System.out.println(regexp); content = content.replaceAll(regexp,""); int len = content.length(); if(len > 120){ return content.substring(0, 120); }else{ return content; } } /** * 字符串切割,比如"#sd#kf##dkf##Ej#"按照"#"切割之后length=8,即"#"出现的次数加一 * @param strObj 目标字符串 * @param str1 用于切割的字符串 * @return String[] */ public static String[] split(String strObj, String delimiter) { if(strObj == null) { return null; } if("".equals(strObj) || "".equals(delimiter)) { return new String[] { strObj}; } int count = org.apache.commons.lang.StringUtils.countMatches(strObj, delimiter) + 1; //调用org.apache.commons int length = delimiter.length(); String[] strs = new String[count]; int posPre = 0 - length; int pos = 0; for(int i = 0; i < count - 1; i++) { pos = strObj.indexOf(delimiter, posPre + length); strs[i] = strObj.substring(posPre + length, pos); posPre = pos; } strs[count - 1] = strObj.substring(posPre + length); return strs; } /** * 用于转义单斜杠特殊字符 * @param str String * @return String */ public static final String appendStr(String str) { char c = ' '; StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length(); i++) { c = str.charAt(i); if(c == '\\') { sb.append("\\\\"); } else { sb.append(c); } } return sb.toString(); } /** * 将reader流中的数据变成String * @param is reader流 * @return String * @throws IOException */ public static String readerToString(Reader is) throws IOException { StringBuffer sb = new StringBuffer(); char[] b = new char[8192]; int n; while((n = is.read(b)) > 0) { sb.append(b, 0, n); } return sb.toString(); } /** * 将input流中的数据变成String * @param is InputStream流 * @throws IOException * @return String */ public static String inputStreamToString(InputStream is) throws IOException { return readerToString(new InputStreamReader(is)); } /** * 返回字符串的前len个字符.例如:输入"abcdefg",3 返回 "abc". * @param value String * @param len int * @return String */ public static String getLmtStr(String value, int len) { if(value == null || value.length() <= len) return value; return value.substring(0, len); } /** * 返回字符串的前len个字符.例如:输入"abcdefg",3 返回 "abc...". * @param value String * @param len int * @return String */ public static String getLmtString(String value, int len) { if(value == null || value.length() <= len) return value; return value.substring(0, len) + "..."; } /** * 输入带html标签的字符串,返回干净的字符串,其中将( ->" "),(<br>->\r\n),(<p>->\r\n\r\n) * @param body String * @return String */ public static String getCleanString(String body) { //替换 ->" ",<br>->\r\n <p>->\r\n\r\n body = body.replaceAll("&[nN][bB][sS][pP];"," "); body = body.replaceAll("<[bB][rR]\\s*>","\r\n"); body = body.replaceAll("<[pP]\\s*>","\r\n\r\n"); //删除所有标签 body = body.replaceAll("<.+?>",""); return body; // String htmlStr = body; //含html标签的字符串 // String textStr = ""; // // // try { // //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> } // String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> } // String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // //定义HTML标签的正则表达式 // String regEx_html = "<[^>]+>"; // //过滤script标签 // java.util.regex.Pattern p_script = java.util.regex.Pattern.compile(regEx_script, java.util.regex.Pattern.CASE_INSENSITIVE); // java.util.regex.Matcher m_script = p_script.matcher(htmlStr); // htmlStr = m_script.replaceAll(""); // //过滤style标签 // java.util.regex.Pattern p_style = java.util.regex.Pattern.compile(regEx_style, java.util.regex.Pattern.CASE_INSENSITIVE); // java.util.regex.Matcher m_style = p_style.matcher(htmlStr); // htmlStr = m_style.replaceAll(""); // //过滤html标签 // java.util.regex.Pattern p_html = java.util.regex.Pattern.compile(regEx_html, java.util.regex.Pattern.CASE_INSENSITIVE); // java.util.regex.Matcher m_html = p_html.matcher(htmlStr); // htmlStr = m_html.replaceAll(""); // // textStr = htmlStr; // // } catch (Exception e) { // // } // // return textStr; //返回文本字符串 } /** * 返回字符串的len个字符.取前后,去掉中间 例如:输入"abcdefg",3 返回 "ab ... g". * @param value String * @param len int * @return String */ public static String getLmtStrx(String value, int len) { if(value == null || value.length() <= len) return value; value = value.substring(0,len/2) + ".." + value.substring(value.length()-len/2); return value; } /** * 给传入的字符串前补足'0',以使字符串长度为len。例如:输入字符串:"23",4 返回:"0023"。 * @param str String * @param len int * @return String */ public String getZeroStr(String str, int len) { int strlen = str.length(); for(int i = 0; i < len - strlen; i++) { str = "0" + str; } return str; } /** * 输入0000返回0001,输入00000001返回00000002 * @param rank String * @return String */ public static String createRank(String rank){ String prefix = null; String suffix = null; // System.out.println("rank"+rank); // if(rank!=null&&rank.trim().endsWith("wf")){ // rank= rank.trim().substring(0,rank.length()-2); // } if (rank.trim().length() > 4) { prefix = rank.substring(0, rank.length() - 4); suffix = rank.substring(prefix.length()); long tmp = Long.parseLong(suffix); tmp++; int len = suffix.length(); suffix = new Long(tmp).toString(); len = len - suffix.length(); for (int n = 0; n < len; n++) { suffix = "0" + suffix; } rank = prefix + suffix; } else { long tmp = Long.parseLong(rank); tmp++; int len = rank.length(); rank = new Long(tmp).toString(); len = len - rank.length(); for (int n = 0; n < len; n++) { rank = "0" + rank; } } return rank; } /** * [ab][cd] - > ab;cd * @param array String[] * @return String */ public static String getArrayAsString(String[] array){ String rs = ""; if(array != null){ for(int i =0;i < array.length;i ++){ rs += array[i] + ";"; } } if(rs.endsWith(";")){ rs = rs.substring(0,rs.length() - 1); } return rs; } /** * 过滤HTML标签 * @param value String * @return String */ public static String filterScriptHtml(String value) { if(value == null) return ""; else { value = value.replaceAll("<", "<"); value = value.replaceAll(">", ">"); } return value; } /** * @param value double * @param format String "0.00" * @return String */ public static String getFormatDouble(double value, String format) { DecimalFormat d = new DecimalFormat(format); return d.format(value); } /** * 0.34567 -> 0.35 * @param value double * @return String */ public static String getFormatDouble(double value) { return getFormatDouble(value, "0.00"); } /** * double数据类型取小数点位数 * @param doubleData double * @param scale int * @return String */ public static String getDoubleByScale(double doubleData,int scale){ String strFormator = "#." + org.apache.commons.lang.StringUtils.repeat("#",scale); java.text.DecimalFormat formater = new java.text.DecimalFormat(strFormator); String newData = formater.format(doubleData); return newData; } /** * 判断str是否在src String[]中 * @param src 源String[] * @param str 要判断的String * @return boolean */ public static boolean isContain(String[] src, String str) { if(null == src) { return false; } for(int i = 0; i < src.length; i++) { if(str.equalsIgnoreCase(src[i])) { return true; } } return false; } /** * 正则表达式判断(忽略大小写) * @param target String * @param regex String * @return boolean */ public static boolean isMatch(String target, String regex) { PatternCompiler compiler = new Perl5Compiler(); Pattern pattern = null; try { pattern = compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK); } catch (Exception e) { return false; } PatternMatcher patternMatcher = new Perl5Matcher(); return patternMatcher.matches(target, pattern); } /** * 产生一个随机id,这个id以系统时间开头 * @return String */ public static String createId() { //System.out.println(System.currentTimeMillis()); String c = "" + getCount(); if (c.length() == 1) { c = "0" + c; } String id = System.currentTimeMillis() + c; int len = 32 - id.length(); return id + org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(len); } private static int counter = 0; protected static int getCount() { synchronized (StringUtils.class) { if (counter < 0) { counter = 0; } counter++; if (counter == 99) { counter = 0; } return counter; } } public static void main(String[] args){ //String b[] = null; ///System.out.print( getArrayAsString(b)); String a = org.trundle.common.FileUtils.read("C:\\Documents and Settings\\admin\\桌面\\aa.txt"); // String abc = " <SPAN lang=EN-US style=\"FONT-SIZE: 12pt; COLOR: #666666; FONT-FAMILY: 宋体; mso-bidi-font-size: 9.0pt\">2003年12月4日<SPAN lang=EN-US style=\"FONT-SIZE:ze:中午 9.0pt\">"; System.out.print(getCleanString(a)); // String you你好 = "你好"; // System.out.println(you你好); // abc = "AA&bb+CC&cc"; // String[] b = abc.split("\\+"); // System.out.println(b[0]); System.out.println(getFormatDouble(4.45,"0")); System.out.println(createRank("00000005")); } }