uTank-木头
每一个你不满意的现在,都有一个你没有努力的曾经。

 

  1 import android.annotation.SuppressLint;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 import java.util.regex.Matcher;
  7 import java.util.regex.Pattern;
  8 
  9 public class StringUtils {
 10     
 11     /**
 12      * 判断ip合法性
 13      */
 14     @SuppressLint("NewApi")
 15     public static boolean isMacthIp(String ip) {
 16         if (ip != null && !ip.isEmpty()) {
 17             // 定义正则表达式            
 18             String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
 19                     + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
 20                     + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
 21                     + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; // 判断ip地址是否与正则表达式匹配            
 22             if (ip.matches(regex)) {           
 23                 return true;            
 24             }
 25         }    
 26         return false;    
 27     }
 28     
 29     /**
 30      * 判断字符串是否为空
 31      */
 32     public static boolean isEmpty(String str){
 33         if (str==null || "".equals(str)){
 34             return true;
 35         }
 36         return false;
 37     }
 38 
 39     /**
 40      * 判断字符串是否为空(包括对"null")
 41      */
 42     public static boolean isNullOrEmpty(String str){
 43         if (str==null || "".equals(str) || "null".equals(str) ){
 44             return true;
 45         }
 46         return false;
 47     }
 48 
 49     
 50     /**
 51      * 判断字符串是否纯数字
 52      */
 53     public static boolean isDigital(String str){
 54         if (!isEmpty(str))
 55             return str.matches("[0-9]+");
 56         return false;
 57     }
 58     
 59     /**
 60      * 计算含有中文的字符串长度
 61      * @param value 字符串(支持含中文字符串)
 62      * @return
 63      */
 64     public static int length(String value) {
 65         int valueLength = 0;
 66         String chinese = "[\u0391-\uFFE5]";
 67         /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
 68         for (int i = 0; i < value.length(); i++) {
 69             /* 获取一个字符 */
 70             String temp = value.substring(i, i + 1);
 71             /* 判断是否为中文字符 */
 72             if (temp.matches(chinese)) {
 73                 /* 中文字符长度为2 */
 74                 valueLength += 2;
 75             } else {
 76                 /* 其他字符长度为1 */
 77                 valueLength += 1;
 78             }
 79         }
 80         return valueLength;
 81     }
 82     
 83     /**
 84      * 字符串数组转换List<String>
 85      * @param items
 86      * @return
 87      */
 88     public static List<String> stringsToList(String[] items) {
 89         List<String> lists = new ArrayList<String>();
 90         for(int i=0; i<items.length; i++){
 91             lists.add(items[i]);
 92         }
 93         return lists;
 94     }
 95 
 96     /**
 97      * 字符串填充,将sour使用fillStr前补或后补满len长度
 98      * @param sour 待填充字符串,支持含有中文
 99      * @param fillStr 填充数据
100       * @param len 填充完整字符串长度
101      * @param isLeft 是否左补填充数据,否则右补填充数据
102      * @return
103      */
104     public static String fill(String sour, String fillStr, int len, boolean isLeft){
105         if (sour == null) {
106             sour = "";
107         }
108         int fillLen = len - length(sour);
109         String fill = "";
110         for (int i=0; i<fillLen; i++) {
111             fill = fill + fillStr;
112         }
113         if (isLeft) {
114             return fill + sour;
115         } else {
116             return sour + fill;
117         }
118     }
119     
120     /**
121      * 字符串填充,中间填充
122      * @param sour 左侧待填充字符串,支持含有中文
123      * @param space 填充的数据,一般为* 、空格、——
124      * @param lengh 填充完整字符串长度
125      * @param modelparams 右侧待填充字符串,支持含有中文
126      * @return
127      */
128     public static String fillMiddle(String sour, String space, int lengh, String modelparams){
129         String s, ss = null;
130         int lenS = sour.length();
131         int lenM = modelparams.length();
132         //做非空处理
133         if(sour == null) {
134             sour = "";
135         }
136         if(modelparams == null) {
137             modelparams = "";
138         }
139         if (space == "") {
140             space = " "; 
141         }
142         //若输入为汉字,则长度取2倍
143         if (sour.matches("[\u4e00-\u9fa5]+")) {
144             lenS = lenS*2;
145         }
146         if (modelparams.matches("[\u4e00-\u9fa5]+")) {
147             lenM = lenM*2;
148         }
149         //若输入有数字则,总长度加3
150         if (sour.matches("[0-9]") && modelparams.matches("[0-9]")) {
151             lengh = lengh + 4;
152         }
153         //长度保护
154         if (lengh < (lenS + lenM)) {
155             new Exception("哥们,长度设置太小了");
156         }
157         
158         s = fill(modelparams, space, lengh - lenS-lenM, true);
159         ss = sour + s +"\n";
160         
161         return ss;
162     }
163     
164     
165     /**
166      * 字符串填充
167      * 
168      * @param strData 待填充字符串,不支持含有中文
169      * @param nLen
170      * @param
171      * @param nOption
172      *            0:左侧填充; 1:右侧填充; 2:两边填充
173      * @return
174      */
175     public static String paddingString(String strData, int nLen, String subStr,
176                                        int nOption) {
177         int i, addCharLen;
178 
179         String strHead = "";
180         String strEnd = "";
181 
182         i = strData.length();
183         if (i >= nLen) {
184             return strData;
185         }
186 
187         switch (nOption) {
188         case 0:
189             addCharLen = (nLen - i) / subStr.length();
190             for (i = 0; i < addCharLen; i++) {
191                 strHead += subStr;
192             }
193             return strHead + strData;
194         case 1:
195             addCharLen = (nLen - i) / subStr.length();
196             for (i = 0; i < addCharLen; i++) {
197                 strEnd += subStr;
198             }
199             return strData + strEnd;
200         case 2:
201             addCharLen = (nLen - i) / (subStr.length() * 2);
202             for (i = 0; i < addCharLen; i++) {
203                 strHead += subStr;
204                 strEnd += subStr;
205             }
206             return strHead + strData + strEnd;
207         default:
208             return strData;
209         }
210     }
211 
212     /**
213      * 整形转换成BCD型的字符串
214      * 9转换成后将变成09,00 09
215      * 19转换后将变成19, 00 19
216      * @param value
217      * @param bytesNum
218      *            BCD字节个数
219      * @return
220      */
221     public static String intToBcd(int value, int bytesNum) {
222         switch(bytesNum){
223         case 1:
224             if (value >= 0  && value <= 99){
225                 return paddingString(String.valueOf(value),2,"0",0);
226             }
227             break;
228         case 2:
229             if (value >= 0  && value <= 999) {
230                 return paddingString(String.valueOf(value),4,"0",0);
231             }
232             break;
233             
234         case 3:
235             if (value >= 0  && value <= 999) {
236                 return paddingString(String.valueOf(value),3,"0",0);
237             }
238             break;
239         }
240         
241         return "";
242     }
243 
244     /**
245      * Hex数据转换成字符串
246      * 
247      * @param value
248      * @return
249      * @throws UnsupportedEncodingException
250      */
251     public static String hexToStr(String value) throws UnsupportedEncodingException {
252         return new String(BytesUtils.hexToBytes(value),"GBK");
253     }
254     /**
255      * Hex数据转换成字符串
256      * encoding 为编码
257      * @param value 
258      * @return
259      * @throws UnsupportedEncodingException
260      */
261     public static String hexToStr(String value, String encoding) throws UnsupportedEncodingException {
262         return new String(BytesUtils.hexToBytes(value),encoding);
263     }
264 
265     /**
266      * 字符串转换成Hex
267      * 
268      * @param value
269      * @return
270      */
271     public static String strToHex(String value) {
272         return BytesUtils.bytesToHex(BytesUtils.getBytes(value));
273     }
274 
275     /**
276      * 往value中填充一个字符0 ,当数据长度正好为2的整数倍时,不填充
277      * 
278      * @param value
279      * @param option
280      *            0:往后填充 ;1:往前填充
281      * @return
282      */
283     public static String paddingZeroToHexStr(String value, int option) {
284         
285         if (value.length() % 2 == 0){
286             return value;
287         }
288         
289         if (option == 0){
290             return "0" + value;
291         }
292         else if (option == 1){
293             return value + "0";
294         }
295         else{
296             return value;
297         }
298     }
299 
300     /**
301      * 判断是否是Hex格式数据
302      * 
303      * @param value
304      * @return
305      */
306     public static boolean checkHexStr(String value) {
307         int i;
308         int len;
309         
310         if (value == null) return false;
311         
312         len = value.length();
313         if (len == 0) return false;
314 
315       for (i= 0;i<len;i++){
316           if (!((value.charAt(i) >= '0' && value.charAt(i) <= '9')|| 
317                   (value.charAt(i) >= 'a' && value.charAt(i) <= 'f') ||
318                   (value.charAt(i) >= 'A' && value.charAt(i) <= 'F'))){
319               return false;               
320           }
321       }
322       return true;
323     }
324 
325     /**
326      * 判断字符串是否是数字0-9
327      * 
328      * @param value
329      * @return
330      */
331 /*    public static boolean checkDigitStr(String value) {
332         int i;
333         int len;
334         
335         if (value == null) return false;
336         
337         len = value.length();
338         if (len == 0) return false;
339 
340       for (i= 0;i<len;i++){
341           if (value.charAt(i) < '0' || value.charAt(i) > '9') {
342               return false;               
343           }
344       }
345       return true;
346     }*/
347     
348     /**
349      * Binary数据转换成Hex
350      * 
351      * @param value
352      * @return
353      */
354     public static String binaryToHex(String value) {
355         int i,j,len;
356         String result ="";
357         char[] hexVocable = { '0', '1', '2', '3', 
358                 '4', '5', '6','7', 
359                 '8', '9', 'A', 'B', 
360                 'C', 'D', 'E', 'F' };
361         String[] binString  = {"0000", "0001", "0010", "0011",
362                 "0100", "0101", "0110", "0111",
363                 "1000", "1001", "1010", "1011",
364                 "1100", "1101", "1110", "1111"};
365         //System.out.println("value: " + value);
366         
367         len = value.length();
368         for(i=0; i<len; i += 4){
369             for(j=0; j<16; j++){
370                 if(binString[j].equals(value.substring(i, i+4))){
371                     result += hexVocable[j];
372                     break;
373                 }                
374             }
375         }
376         //System.out.println("result: " + result);
377         return result;
378     }
379 
380     /**
381      * Hex数据转换成Binary
382      * 
383      * @param value
384      * @return
385      */
386     public static String hexToBinary(String value) {
387         int i,j,len;
388         String result ="";
389         char[] hexVocable = { '0', '1', '2', '3', 
390                 '4', '5', '6','7', 
391                 '8', '9', 'A', 'B', 
392                 'C', 'D', 'E', 'F' };
393         String[] binString  = {"0000", "0001", "0010", "0011",
394                 "0100", "0101", "0110", "0111",
395                 "1000", "1001", "1010", "1011",
396                 "1100", "1101", "1110", "1111"};
397         
398         len = value.length();
399         for(i=0; i<len; i++){
400             for(j=0; j<16; j++){
401                 if(value.charAt(i) == hexVocable[j]){
402                     result += binString[j];
403                     break;
404                 }                
405             }
406         }
407         //System.out.println("result: " + result);
408         return result;
409     }
410     
411     /**
412      * 获取二进制字符串
413      * 0x00 0x01 0x00 0x01 0x01转换成"01011"
414      * @param value
415      * @return
416      */
417     public static String getBinaryString(byte[] value) {
418         int len;
419         String result ="";
420         
421         len = value.length;
422         
423         for(int i=0;i<len;i++) {
424             result += String.valueOf(value[i]);
425         }
426         
427         return result;
428     }
429     /**
430      * mmdd日期格式
431      * @param date
432      * @return
433      */
434     public static boolean isMatchDate(String date){
435         if(date.length()!=4||date==null){
436             return false;
437         }
438         String eL="(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-9])))";
439            Pattern p = Pattern.compile(eL);
440             Matcher m = p.matcher(date);
441             boolean b = m.matches();   
442             return b;
443     }
444     
445     /**
446      * 删除字符串左侧所有字符ch
447      * @param
448      * @return
449      */
450     public static String deleteLeftChar(String data, char ch){
451         int flag = 0;
452         for(int i = 0;i<data.length();i++){
453             if(data.charAt(i) == ch){
454                 flag = i;
455             }else{
456                 break;
457             }
458         }
459         flag = flag+1;
460         data = data.substring(flag);
461         return data;
462     }
463     
464     /**
465      * 删除字符串右侧所有字符ch
466      * @param
467      * @return
468      */
469     public static String deleteRightChar(String data, char ch){
470         
471         int flag = data.length();
472         for(int i = data.length()-1;i>=0;i--){
473             if(data.charAt(i) == ch){
474                 flag = i;
475             }else{
476                 break;
477             }
478         }
479 
480         data = data.substring(0,flag);
481         return data;
482     }
483     
484     
485         /**
486      * 将显示金额的字符串(带原点),转化为没有原点的字符串
487      * 
488      * 示例:123.32 --->  12332
489      *       2.  ----> 200
490      *       2.3  ----> 230
491      *       0.12 --->  12
492      *       0.02 --->   2
493      * @param str
494      * @return
495      */
496     public static String decimalWipeDot(String str){
497         
498         //拿到字符串
499         if (str.contains(".")) {
500             
501             //判断点后还有几位
502             int indexOf = str.indexOf(".");
503             //用索引和长度进行判断
504             int length = str.length();
505             int cha = length - indexOf; 
506             char charAt = str.charAt(indexOf - 1);
507 
508             switch (cha) {
509             case 1:
510                 //小数后有0位--先去掉点,在加个00   eg: 2. --> 200   0.-->0
511                 if (charAt == '0') {
512                     str= "0";
513                 }else {
514                     str = str.replace(".", "");
515                     str= str+"00";
516                 }
517                 break;
518             case 2:
519                 //小数后有1位--先去掉点,在加个0  eg: 2.3 -->  230  0.1 -->10
520                 if (charAt == '0') {
521                     str= str.charAt(indexOf + 1)+"0";
522                 }else {
523                     str = str.replace(".", "");
524                     str= str+"0";
525                 }
526                 
527                 break;
528             case 3:
529                 //小数后有2位--直接去掉点   eg:   2.03 --> 203   0.12-->12  0.02 --> 2
530                 char charAt2 = str.charAt(indexOf + 1);
531                 if (str.length() == 4) {
532                     
533                     if (charAt == '0') {
534                         if (charAt2 == '0') {
535                             str= str.substring(length - 1,length);
536                         }else {
537                             
538                             str= str.substring(indexOf + 1,length);
539                         }
540                     }else {
541                         str = str.replace(".", "");
542                     }
543                 }else {
544                     str = str.replace(".", "");
545                 }
546                 
547                 break;
548             default:
549                 break;
550             }
551         }else {
552             //没有点, 直接加00  eg:  23 ---> 2300   0--->0   0000023--->  2300    00000--->0
553             //去掉前面多余的0 ,在判断这个数是否为0
554             if (str.equals("")) {
555                 str = "0";
556             }
557             int int1 = Integer.parseInt(str);
558             str = String.valueOf(int1);
559             
560             if (int1 != 0){
561                 str = str+"00";
562             }else {
563                 str = "0";
564             }
565         }
566         return str;
567     }
568     
569     /**
570      * 将金额的字符串(不带点)  转化为带点的金额字符串
571      * 示例       12332 ---> 123.32
572      *       200   --->  2
573      *       230   ----> 2.3 
574      *        12 ---> 0.12 
575      *         2  ---> 0.02
576      * 
577      * 
578      * @param str
579      * @return
580      */
581     public static String decimalAddDot(String str){
582         boolean isNegative = false;
583         str = str.replace(" ","");
584 
585         if (str == null || str == "") {
586             return "0.00";
587         }
588         //截取字符第一个字符
589          String sign = str.substring(0,1);
590         if (sign.equals("-")) {
591             isNegative = true;//标记为负数
592             str = str.substring(1,str.length());
593         }else{
594             //正数不作处理
595         }
596 
597         int length = str.length();
598         if (length >= 3) {  //200-->2.00
599             str = str.substring(0, length-2)+"."+str.substring(length-2, length);
600         }else {
601             switch (length) {
602             case 2:// 20-->0.20   23 --> 0.23
603                 str = "0."+str;
604                 break;
605             case 1://  2-->0.02   
606                 str = "0.0"+str;
607                 break;
608             case 0:
609                 //说明没有    0.00
610                 str = "0.00";
611                 break;
612             default:
613                 break;
614             }
615         }
616 
617         if (isNegative) {
618             return "-"+str;
619         }else{
620             return str;
621         }
622 
623     
624         
625     }
626 }

 

posted on 2017-11-22 09:50  uTank  阅读(2429)  评论(0编辑  收藏  举报