总结-处理字符串常用方法类

来源:互联网 发布:php的优点和缺点 编辑:程序博客网 时间:2024/05/20 04:49

WEB工程上都在操作String字符串数据

根据工程的需要,写了很多处理字符串的方法

现在总结了一下最常用的字符串处理

 

StringUtil.java

Java代码 复制代码
  1. package com.chyi.core.utils;   
  2.   
  3. import java.text.DecimalFormat;   
  4. import java.util.List;   
  5. import java.util.UUID;   
  6. import java.util.regex.Pattern;   
  7.   
  8. /**  
  9.  * 描述:字符串处理类的常用方法  
  10.  * @author 齐继超  
  11.  *  
  12.  */  
  13. public class StringUtil {    
  14.        
  15.     /**  
  16.      * 描述:取得32位的通用标识UUID  
  17.      * @return   
  18.      * 作者:齐继超  
  19.      * 时间:Nov 27, 2010  
  20.      */  
  21.     public static String getUUID(){   
  22.         return UUID.randomUUID().toString().replaceAll("-""");   
  23.     }   
  24.        
  25.     /**  
  26.      * 描述:将没有实例化的字符串转换成空字符串  
  27.      * @param str  
  28.      * @return  
  29.      * 作者:齐继超  
  30.      * 时间:Nov 27, 2010  
  31.      */  
  32.     public static String convertNull(String str){   
  33.         if (str==null) {   
  34.             return "";   
  35.         }else {   
  36.             return str;   
  37.         }   
  38.     }   
  39.   
  40.        
  41.     /**  
  42.      * 描述:Object类转换为String,避免在Object为null时,直接toString()出错  
  43.      * @param obj  
  44.      * @return  
  45.      * 作者:齐继超  
  46.      * 时间:Nov 29, 2010  
  47.      */  
  48.     public static String obj2Str(Object obj) {   
  49.         return obj == null ? null : obj.toString();   
  50.     }   
  51.        
  52.     /**  
  53.      * 描述:判断为空,返回true  
  54.      * @param str  
  55.      * @return   
  56.      * 作者:齐继超  
  57.      * 时间:Nov 27, 2010  
  58.      */  
  59.     public static boolean isBlank(String str){   
  60.         if(null == str || "".equals(str.trim())){   
  61.             return true;   
  62.         }   
  63.         return false;   
  64.     }   
  65.        
  66.     /**  
  67.      * 描述:接收一个list,如果有一个为空,返回true  
  68.      * @param str  
  69.      * @return   
  70.      * 作者:齐继超  
  71.      * 时间:Nov 27, 2010  
  72.      */  
  73.     public static boolean isBlank(List<String> str){   
  74.         for (String string : str) {   
  75.             if (isBlank(string)) {   
  76.                 return true;   
  77.             }   
  78.         }   
  79.         return false;   
  80.     }   
  81.        
  82.     /**  
  83.      * 描述:不为空,返回true  
  84.      * @param str  
  85.      * @return  
  86.      * 作者:齐继超  
  87.      * 时间:Nov 27, 2010  
  88.      */  
  89.     public static boolean isNotBlank(String str){   
  90.         return !isBlank(str);   
  91.     }   
  92.        
  93.     /**  
  94.      * 描述:接收一个list,如果都不为空,返回true  
  95.      * @param list  
  96.      * @return  
  97.      * 作者:齐继超  
  98.      * 时间:Nov 27, 2010  
  99.      */  
  100.     public static boolean isNotBlank(List<String> list){   
  101.         return !isBlank(list);   
  102.     }   
  103.        
  104.     /**  
  105.      * 描述:将textarea输入的文本,转换成HTML格式输出  
  106.      * @return   
  107.      * 作者:齐继超  
  108.      * 时间:Nov 27, 2010  
  109.      */  
  110.     public static String textareaToHtml(String text){   
  111.         if (text!=null) {   
  112.             return text.replaceAll("/r/n""<br>").replaceAll(" ""&nbsp;");   
  113.         }else {   
  114.             return null;   
  115.         }   
  116.     }   
  117.        
  118.     /**  
  119.      * 描述:过滤HTML标签  
  120.      * @param str  
  121.      * @return  
  122.      * 作者:齐继超  
  123.      * 时间:Nov 29, 2010  
  124.      */  
  125.     public static String filterHtml(String str){   
  126.         if (str==null) {   
  127.             return "";   
  128.         }else {   
  129.             str = str.replaceAll("<""&lt;");      
  130.             str = str.replaceAll(">""&gt;");   
  131.         }   
  132.         return str;   
  133.      }   
  134.        
  135.     /**  
  136.      * 描述:全数字判断  
  137.      * @param str  
  138.      * @return   
  139.      * 作者:齐继超  
  140.      * 时间:Nov 27, 2010  
  141.      */  
  142.     public static boolean isNumber(String strIn){   
  143.         return isNumber(strIn,"0123456789");   
  144.     }   
  145.        
  146.     /**  
  147.      * 描述:全数字判断,strRef 参照字可以自定义  
  148.      * @param strIn  
  149.      * @param strRef  参照字  
  150.      * @return   
  151.      * 作者:齐继超  
  152.      * 时间:Nov 27, 2010  
  153.      */  
  154.     public static boolean isNumber(String strIn,String strRef){   
  155.         if (strIn== null || strIn.length()==0) {   
  156.             return false;   
  157.         }   
  158.         for (int i = 0; i < strIn.length(); i++) {   
  159.             String strTemp = strIn.substring(i, i+1);   
  160.             if (strRef.indexOf(strTemp, 0) == -1) {   
  161.                 return false;   
  162.             }   
  163.         }   
  164.         return true;   
  165.     }   
  166.        
  167.     /**  
  168.      * 描述:判断是否是汉字  
  169.      * @param str  
  170.      * @return  
  171.      * 作者:齐继超  
  172.      * 时间:Nov 29, 2010  
  173.      */  
  174.     public static boolean isHanzi(String str) {   
  175.       char[] chars=str.toCharArray();      
  176.         boolean isGB2312=false;      
  177.         for(int i=0;i<chars.length;i++){     
  178.             byte[] bytes=(""+chars[i]).getBytes();      
  179.             if(bytes.length==2){      
  180.                 int[] ints=new int[2];      
  181.                 ints[0]=bytes[0]& 0xff;      
  182.                 ints[1]=bytes[1]& 0xff;      
  183.                 if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){      
  184.                     isGB2312=true;      
  185.                     break;      
  186.                 }      
  187.             }      
  188.         }      
  189.          return isGB2312;   
  190.   
  191.     }   
  192.        
  193.     /**  
  194.      * 描述:是否包含汉字  
  195.      * @param str  
  196.      * @return  
  197.      * 作者:齐继超  
  198.      * 时间:Nov 29, 2010  
  199.      */  
  200.     public static boolean isHasHanz(String str){   
  201.         int strLen = new String(str).length();   
  202.         return (str.getBytes().length == strLen)?false:true;   
  203.     }   
  204.        
  205.     /**  
  206.      * 描述:判断是否为浮点型  
  207.      * @return  
  208.      * 作者:齐继超  
  209.      * 时间:Nov 29, 2010  
  210.      */  
  211.     public static boolean isFloat(String strIn){   
  212.         if (strIn== null || strIn.length()==0)   
  213.             return false ;   
  214.         Pattern pattern = Pattern.compile("[0-9]*(//.?)[0-9]*");   
  215.         return pattern.matcher(strIn).matches();   
  216.     }   
  217.        
  218.        
  219.     /**  
  220.      * 描述:电话号码格式化 例如13233333333 return 132****3333  
  221.      * @param tel  
  222.      * @return  
  223.      * 作者:齐继超  
  224.      * 时间:Nov 29, 2010  
  225.      */  
  226.     public static String telReplaceXX(String tel) {   
  227.         if (org.apache.commons.lang.StringUtils.isBlank(tel)) {   
  228.             return null;   
  229.         }   
  230.         char[] strs = tel.toCharArray();   
  231.         for (int i = 3; i < 7; i++) {   
  232.             strs[i] = '*';   
  233.         }   
  234.         return new String(strs);   
  235.     }   
  236.        
  237.     /**  
  238.      * 描述:阿拉伯数值转换成汉字  
  239.      * @param a  
  240.      * @return  
  241.      * 作者:齐继超  
  242.      * 时间:Nov 29, 2010  
  243.      */  
  244.     public static String numToHanz(int a) {   
  245.         String[] units = { """十""百""千""万""十""百""千""亿" };   
  246.         String[] nums = { "一""二""三""四""五""六""七""八""九""十" };   
  247.         String result = "";   
  248.         if (a < 0) {   
  249.             result = "负";   
  250.             a = Math.abs(a);   
  251.         }   
  252.         String t = String.valueOf(a);   
  253.         for (int i = t.length() - 1; i >= 0; i--) {   
  254.             int r = (int) (a / Math.pow(10, i));   
  255.             if (r % 10 != 0) {   
  256.                 String s = String.valueOf(r);   
  257.                 String l = s.substring(s.length() - 1, s.length());   
  258.                 result += nums[Integer.parseInt(l) - 1];   
  259.                 result += (units[i]);   
  260.             } else {   
  261.                 if (!result.endsWith("零")) {   
  262.                     result += "零";   
  263.                 }   
  264.             }   
  265.         }   
  266.         return result;   
  267.     }   
  268.        
  269.     /**  
  270.      * 描述:字符串替换,  
  271.      * @param strObj  
  272.      * @param str1  
  273.      * @param str2  
  274.      * @return  
  275.      * 作者:齐继超  
  276.      * 时间:Nov 27, 2010  
  277.      */  
  278.     public static String replace(String strObj,String str1,String str2){   
  279.         if ("".equals(str1)){   
  280.             return strObj;   
  281.         }   
  282.         return org.apache.commons.lang.StringUtils.replace(strObj, str1, str2);   
  283.     }   
  284.        
  285.     /**  
  286.      * 描述:根据内容截取摘要  
  287.      * @param str    
  288.      * @param subLen   
  289.      * @return  
  290.      * 作者:齐继超  
  291.      * 时间:Nov 29, 2010  
  292.      */  
  293.     public static String subSummary(String str,int subLen){   
  294.         if (str==null)return null;   
  295.         String regexp="<.*?>";   
  296.         str = str.replace(regexp, "");   
  297.         int len = str.length();   
  298.         if (len > subLen) {   
  299.             return str.substring(0, subLen);   
  300.         }else {   
  301.             return str;   
  302.         }   
  303.     }   
  304.        
  305.     /**  
  306.      * 描述:描述:截取字符串省略代替... 例如:输入"abcdefg",3 返回 "abc...".    
  307.      * @param str  
  308.      * @param len  
  309.      * @param isOmit 是否省略...  
  310.      * @return  
  311.      * 作者:齐继超  
  312.      * 时间:Nov 29, 2010  
  313.      */  
  314.     public static String subString(String str,int len,boolean isOmit){   
  315.         if(str==null || str.length()<=len)   
  316.             return str;   
  317.         if (isOmit) {   
  318.             return str.substring(0, len)+"...";    
  319.         }else {   
  320.             return str.substring(0, len);   
  321.         }   
  322.     }   
  323.        
  324.      /**  
  325.      * 描述:返回字符串的len个字符.取前后,去掉中间 例如:输入"abcdefg",3 返回 "ab ... g".    
  326.      * @param value  
  327.      * @param len  
  328.      * @return  
  329.      * 作者:齐继超  
  330.      * 时间:Nov 29, 2010  
  331.      */  
  332.     public static String getLmtStrx(String value, int len) {      
  333.             if(value == null || value.length() <= len)      
  334.               return value;      
  335.             value = value.substring(0,len/2) + ".." + value.substring(value.length()-len/2);      
  336.             return value;      
  337.     }      
  338.        
  339.        
  340.     /**  
  341.      * 描述:字符串切割,比如"2010-11-29" 按照"-"  
  342.      * @param strObj  
  343.      * @param delimiter  
  344.      * @return  
  345.      * 作者:齐继超  
  346.      * 时间:Nov 29, 2010  
  347.      */  
  348.     public static String[] split(String strObj, String delimiter) {      
  349.         if(strObj == null) {      
  350.           return null;      
  351.         }      
  352.         if("".equals(strObj) || "".equals(delimiter)) {      
  353.           return new String[] {      
  354.               strObj};      
  355.         }      
  356.          
  357.         int count = org.apache.commons.lang.StringUtils.countMatches(strObj,      
  358.             delimiter) + 1//调用org.apache.commons      
  359.         int length = delimiter.length();      
  360.         String[] strs = new String[count];      
  361.         int posPre = 0 - length;      
  362.         int pos = 0;      
  363.         for(int i = 0; i < count - 1; i++) {      
  364.           pos = strObj.indexOf(delimiter, posPre + length);      
  365.           strs[i] = strObj.substring(posPre + length, pos);      
  366.           posPre = pos;      
  367.         }      
  368.         strs[count - 1] = strObj.substring(posPre + length);      
  369.         return strs;      
  370.       }    
  371.        
  372.     /**  
  373.      * 描述:给传入的字符串前补足'0',以使字符串长度为len。例如:输入字符串:"23",4 返回:"0023"。    
  374.      * @param str  
  375.      * @param len  
  376.      * @return  
  377.      * 作者:齐继超  
  378.      * 时间:Nov 29, 2010  
  379.      */  
  380.     public static String zeroStr(String str,int len){   
  381.         int strLen= str.length();   
  382.         for (int i = 0; i < len-strLen; i++) {   
  383.             str = "0"+str;   
  384.         }   
  385.         return str;   
  386.     }   
  387.        
  388.     /**  
  389.      * 描述:输入0000返回0001,输入00000001返回00000002    
  390.      * @param rank  
  391.      * @return  
  392.      * 作者:齐继超  
  393.      * 时间:Nov 29, 2010  
  394.      */  
  395.     public static String createRank(String rank){      
  396.           String prefix = null;      
  397.           String suffix = null;      
  398.           if (rank.trim().length() > 4) {      
  399.               prefix = rank.substring(0, rank.length() - 4);      
  400.               suffix = rank.substring(prefix.length());      
  401.               long tmp = Long.parseLong(suffix);      
  402.               tmp++;      
  403.               int len = suffix.length();      
  404.               suffix = new Long(tmp).toString();      
  405.               len = len - suffix.length();      
  406.               for (int n = 0; n < len; n++) {      
  407.                   suffix = "0" + suffix;      
  408.               }      
  409.               rank = prefix + suffix;      
  410.           } else {      
  411.               long tmp = Long.parseLong(rank);      
  412.               tmp++;      
  413.               int len = rank.length();      
  414.               rank = new Long(tmp).toString();      
  415.               len = len - rank.length();      
  416.               for (int n = 0; n < len; n++) {      
  417.                   rank = "0" + rank;      
  418.               }      
  419.           }      
  420.           return rank;      
  421.       }      
  422.   
  423.        
  424.      /**  
  425.      * 描述:浮点格式化 例如value=45.678,format=000.00,返回值就是045.68  
  426.      * @param value  
  427.      * @param format  
  428.      * @return  
  429.      * 作者:齐继超  
  430.      * 时间:Nov 29, 2010  
  431.      */  
  432.     public static String getFormatDouble(double value, String format) {      
  433.             DecimalFormat d = new DecimalFormat(format);      
  434.             return d.format(value);      
  435.     }   
  436.        
  437.      /**  
  438.      * 描述:浮点格式化 value = 45.678 返回值 45.68  
  439.      * @param value  
  440.      * @return  
  441.      * 作者:齐继超  
  442.      * 时间:Nov 29, 2010  
  443.      */  
  444.     public static String getFormatDouble(double value) {      
  445.             return getFormatDouble(value, "0.00");      
  446.      }    
  447.        
  448.      /**  
  449.      * 描述:double数据类型取小数点位数  
  450.      * 例如:doubleData = 45.678,scale = 2 返回值 = 45.68  
  451.      * @param doubleData  
  452.      * @param scale  
  453.      * @return  
  454.      * 作者:齐继超  
  455.      * 时间:Nov 29, 2010  
  456.      */  
  457.     public static String  getDoubleByScale(double doubleData,int scale){      
  458.             String strFormator = "#." + org.apache.commons.lang.StringUtils.repeat("#",scale);      
  459.             java.text.DecimalFormat formater = new java.text.DecimalFormat(strFormator);      
  460.             String newData = formater.format(doubleData);      
  461.             return newData;      
  462.      }   
  463.        
  464.     /**  
  465.      * 描述:判断str是否在arr String[]中  
  466.      * @param arr  
  467.      * @param str  
  468.      * @return  
  469.      * 作者:齐继超  
  470.      * 时间:Nov 29, 2010  
  471.      */  
  472.     public static boolean isContain(String[] arr,String str){   
  473.         if (null==arr) {   
  474.             return false;   
  475.         }   
  476.         for (int i = 0; i < arr.length; i++) {   
  477.             //equalsIgnoreCase 不区分大小写   
  478.             if (str.equalsIgnoreCase(arr[i])) {   
  479.                 return true;   
  480.             }   
  481.         }   
  482.         return false;   
  483.            
  484.     }   
  485.        
  486.     /**  
  487.      * 描述:获取sub在string里的个数,例如 sub=123,string=123123,return=2  
  488.      * @param sub  
  489.      * @param string  
  490.      * @return  
  491.      * 作者:齐继超  
  492.      * 时间:Nov 29, 2010  
  493.      */  
  494.     public static int getSubCount(String sub, String string) {   
  495.         if (isBlank(string)) {   
  496.             return 0;   
  497.         }   
  498.         int subLength = sub.length();   
  499.         int forLength = string.length() - sub.length() + 1// 循环次数   
  500.         int count = 0// 记录循环次数   
  501.         for (int i = 0; i < forLength; i++) {   
  502.             if (org.apache.commons.lang.StringUtils.substring(string, i, i + subLength).equals(sub)) {   
  503.                 count++;   
  504.             }   
  505.         }   
  506.         return count;   
  507.     }   
  508.        
  509.     /**  
  510.      * 描述:数组转换为字符串  
  511.      * @param array  
  512.      * @return  
  513.      * 作者:齐继超  
  514.      * 时间:Nov 29, 2010  
  515.      */  
  516.     public static String getArrayAsString(String[] array){      
  517.         String rs = "";      
  518.         if(array != null){      
  519.           for(int i =0;i < array.length;i ++){      
  520.             rs += array[i] + ";";      
  521.           }      
  522.         }      
  523.         //测试此字符串是否以指定的后缀结束   
  524.         if(rs.endsWith(";")){      
  525.           rs = rs.substring(0,rs.length() - 1);      
  526.         }      
  527.         return rs;      
  528.       }     
  529.        
  530.     /**  
  531.      * 描述:输入带html标签的字符串,返回干净的字符串  
  532.      * @param body  
  533.      * @return  
  534.      * 作者:齐继超  
  535.      * 时间:Nov 29, 2010  
  536.     */  
  537.     public static String getCleanString(String body) {      
  538.         //替换&nbsp;->" ",<br>->/r/n <p>->/r/n/r/n      
  539.         body = body.replaceAll("&[nN][bB][sS][pP];"," ");      
  540.         body = body.replaceAll("<[bB][rR]//s*>","/r/n");      
  541.         body = body.replaceAll("<[pP]//s*>","/r/n/r/n");      
  542.         //删除所有标签      
  543.         body = body.replaceAll("<.+?>","");      
  544.         return body;      
  545.       }      
  546.   
  547. }  
原创粉丝点击