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 }
