| 1 3 package jodd.util; 4 5 import jodd.typeconverter.StringArrayConverter; 6 7 import java.io.UnsupportedEncodingException ; 8 9 10 13 public class StringUtil { 14 15 public static final String WHITESPACE = " \n\r\f\t"; 16 public static final String EMPTY_STRING = ""; 17 18 20 28 public static String replace(String s, String sub, String with) { 29 int c = 0; 30 int i = s.indexOf(sub, c); 31 if (i == -1) { 32 return s; 33 } 34 int sLen = s.length(); 35 StringBuffer buf = new StringBuffer (sLen + with.length()); 36 do { 37 buf.append(s.substring(c,i)); 38 buf.append(with); 39 c = i + sub.length(); 40 } while ((i = s.indexOf(sub, c)) != -1); 41 if (c < sLen) { 42 buf.append(s.substring(c, sLen)); 43 } 44 return buf.toString(); 45 } 46 47 54 public static String replace(String s, char sub, char with) { 55 char[] str = s.toCharArray(); 56 for (int i = 0; i < str.length; i++) { 57 if (str[i] == sub) { 58 str[i] = with; 59 } 60 } 61 return new String (str); 62 } 63 64 71 public static String replaceFirst(String s, String sub, String with) { 72 int i = s.indexOf(sub); 73 if (i == -1) { 74 return s; 75 } 76 return s.substring(0, i) + with + s.substring(i + sub.length()); 77 } 78 79 86 public static String replaceFirst(String s, char sub, char with) { 87 char[] str = s.toCharArray(); 88 for (int i = 0; i < str.length; i++) { 89 if (str[i] == sub) { 90 str[i] = with; 91 break; 92 } 93 } 94 return new String (str); 95 } 96 97 104 public static String replaceLast(String s, String sub, String with) { 105 int i = s.lastIndexOf(sub); 106 if (i == -1) { 107 return s; 108 } 109 return s.substring(0, i) + with + s.substring(i + sub.length()); 110 } 111 112 119 public static String replaceLast(String s, char sub, char with) { 120 char[] str = s.toCharArray(); 121 for (int i = str.length - 1; i >= 0; i--) { 122 if (str[i] == sub) { 123 str[i] = with; 124 break; 125 } 126 } 127 return new String (str); 128 } 129 130 132 138 public static String remove(String s, String sub) { 139 int c = 0; 140 int sublen = sub.length(); 141 if (sublen == 0) { 142 return s; 143 } 144 int i = s.indexOf(sub, c); 145 if (i == -1) { 146 return s; 147 } 148 StringBuffer buf = new StringBuffer (s.length()); 149 do { 150 buf.append(s.substring(c, i)); 151 c = i + sublen; 152 } while ((i = s.indexOf(sub, c)) != -1); 153 if (c < s.length()) { 154 buf.append(s.substring(c, s.length())); 155 } 156 return buf.toString(); 157 } 158 159 165 public static String removeChars(String src, String chars) { 166 int i = src.length(); 167 StringBuffer stringbuffer = new StringBuffer (i); 168 for (int j = 0; j < i; j++) { 169 char c = src.charAt(j); 170 if (chars.indexOf(c) == -1) { 171 stringbuffer.append(c); 172 } 173 } 174 return stringbuffer.toString(); 175 } 176 177 178 184 public static String removeChars(String src, char[] chars) { 185 int i = src.length(); 186 StringBuffer stringbuffer = new StringBuffer (i); 187 mainloop: 188 for (int j = 0; j < i; j++) { 189 char c = src.charAt(j); 190 for (int k = 0; k < chars.length; k++) { 191 if (c == chars[k]) { 192 continue mainloop; 193 } 194 } 195 stringbuffer.append(c); 196 } 197 return stringbuffer.toString(); 198 } 199 200 206 public static String remove(String src, char chars) { 207 int i = src.length(); 208 StringBuffer stringbuffer = new StringBuffer (i); 209 for (int j = 0; j < i; j++) { 210 char c = src.charAt(j); 211 if (c == chars) { 212 continue; 213 } 214 stringbuffer.append(c); 215 } 216 return stringbuffer.toString(); 217 } 218 219 221 230 public static boolean equals(String s1, String s2) { 231 return ObjectUtil.equals(s1, s2); 232 } 233 234 237 public static boolean isEmpty(String string) { 238 return ((string == null) || (string.length() == 0)); 239 } 240 241 244 public static boolean isBlank(String string) { 245 return ((string == null) || (string.trim().length() == 0)); 246 } 247 248 251 public static boolean isNotEmpty(String string) { 252 return string != null && string.length() > 0; 253 } 254 255 256 260 public static String toString(Object obj) { 261 if (obj == null) { 262 return null; 263 } 264 return obj.toString(); 265 } 266 267 271 public static String toNotNullString(Object obj) { 272 if (obj == null) { 273 return EMPTY_STRING; 274 } 275 return obj.toString(); 276 } 277 278 281 public static String [] toStringArray(Object obj) { 282 return StringArrayConverter.valueOf(obj); 283 } 284 285 286 288 295 public static String capitalize(String str) { 296 return changeFirstCharacterCase(true, str); 297 } 298 299 307 public static String uncapitalize(String str) { 308 return changeFirstCharacterCase(false, str); 309 } 310 311 317 private static String changeFirstCharacterCase(boolean capitalize, String str) { 318 int strLen = str.length(); 319 if (strLen == 0) { 320 return str; 321 } 322 StringBuffer buf = new StringBuffer (strLen); 323 if (capitalize) { 324 buf.append(Character.toUpperCase(str.charAt(0))); 325 } else { 326 buf.append(Character.toLowerCase(str.charAt(0))); 327 } 328 buf.append(str.substring(1)); 329 return buf.toString(); 330 } 331 332 333 335 336 339 public static String truncate(String string, int length) { 340 if (string.length() > length) { 341 string = string.substring(0, length); 342 } 343 return string; 344 } 345 346 348 363 public static String [] split(String src, String delimeter) { 364 int maxparts = (src.length() / delimeter.length()) + 2; int[] positions = new int[maxparts]; 366 int dellen = delimeter.length(); 367 368 int i, j = 0; 369 int count = 0; 370 positions[0] = - dellen; 371 while ((i = src.indexOf(delimeter, j)) != -1) { 372 count++; 373 positions[count] = i; 374 j = i + dellen; 375 } 376 count++; 377 positions[count] = src.length(); 378 379 String [] result = new String [count]; 380 381 for (i = 0; i < count; i++) { 382 result[i] = src.substring(positions[i] + dellen, positions[i + 1]); 383 } 384 return result; 385 } 386 387 397 public static String [] splitc(String src, String d) { 398 if ((d.length() == 0) || (src.length() == 0) ) { 399 return new String [] {src}; 400 } 401 char[] delimiters = d.toCharArray(); 402 char[] srcc = src.toCharArray(); 403 404 int maxparts = srcc.length + 1; 405 int[] start = new int[maxparts]; 406 int[] end = new int[maxparts]; 407 408 int count = 0; 409 410 start[0] = 0; 411 int s = 0, e; 412 if (CharUtil.equalsOne(srcc[0], delimiters) == true) { end[0] = 0; 414 count++; 415 s = CharUtil.findFirstDiff(srcc, 1, delimiters); 416 if (s == -1) { return new String [] {EMPTY_STRING, EMPTY_STRING}; 418 } 419 start[1] = s; } 421 while (true) { 422 e = CharUtil.findFirstEqual(srcc, s, delimiters); 424 if (e == -1) { 425 end[count] = srcc.length; 426 break; 427 } 428 end[count] = e; 429 430 count++; 432 s = CharUtil.findFirstDiff(srcc, e, delimiters); 433 if (s == -1) { 434 start[count] = end[count] = srcc.length; 435 break; 436 } 437 start[count] = s; 438 } 439 count++; 440 String [] result = new String [count]; 441 for (int i = 0; i < count; i++) { 442 result[i] = src.substring(start[i], end[i]); 443 } 444 return result; 445 } 446 447 457 public static String [] splitc(String src, char delimiter) { 458 char[] srcc = src.toCharArray(); 459 460 int maxparts = srcc.length + 1; 461 int[] start = new int[maxparts]; 462 int[] end = new int[maxparts]; 463 464 int count = 0; 465 466 start[0] = 0; 467 int s = 0, e; 468 if (srcc[0] == delimiter) { end[0] = 0; 470 count++; 471 s = CharUtil.findFirstDiff(srcc, 1, delimiter); 472 if (s == -1) { return new String [] {EMPTY_STRING, EMPTY_STRING}; 474 } 475 start[1] = s; } 477 while (true) { 478 e = CharUtil.findFirstEqual(srcc, s, delimiter); 480 if (e == -1) { 481 end[count] = srcc.length; 482 break; 483 } 484 end[count] = e; 485 486 count++; 488 s = CharUtil.findFirstDiff(srcc, e, delimiter); 489 if (s == -1) { 490 start[count] = end[count] = srcc.length; 491 break; 492 } 493 start[count] = s; 494 } 495 count++; 496 String [] result = new String [count]; 497 for (int i = 0; i < count; i++) { 498 result[i] = src.substring(start[i], end[i]); 499 } 500 return result; 501 } 502 503 504 506 517 public static int indexOf(String src, String sub, int startIndex, int endIndex) { 518 int sublen = sub.length(); 519 if (sublen == 0) { 520 return startIndex; 521 } 522 int total = endIndex - sublen + 1; 523 char c = sub.charAt(0); 524 mainloop: 525 for (int i = startIndex; i < total; i++) { 526 if (src.charAt(i) != c) { 527 continue; 528 } 529 int j = 1; 530 int k = i + 1; 531 while (j < sublen) { 532 if (sub.charAt(j) != src.charAt(k)) { 533 continue mainloop; 534 } 535 j++; k++; 536 } 537 return i; 538 } 539 return -1; 540 } 541 542 545 public static int indexOf(String src, char c, int startIndex, int endIndex) { 546 for (int i = startIndex; i < endIndex; i++) { 547 if (src.charAt(i) == c) { 548 return i; 549 } 550 } 551 return -1; 552 } 553 554 555 556 565 public static int indexOfIgnoreCase(String src, String subS) { 566 return indexOfIgnoreCase(src, subS, 0, src.length()); 567 } 568 569 582 public static int indexOfIgnoreCase(String src, String subS, int startIndex) { 583 return indexOfIgnoreCase(src, subS, startIndex, src.length()); 584 } 585 596 public static int indexOfIgnoreCase(String src, String sub, int startIndex, int endIndex) { 597 int sublen = sub.length(); 598 if (sublen == 0) { 599 return startIndex; 600 } 601 sub = sub.toLowerCase(); 602 int total = endIndex - sublen + 1; 603 char c = sub.charAt(0); 604 mainloop: 605 for (int i = startIndex; i < total; i++) { 606 if (Character.toLowerCase(src.charAt(i)) != c) { 607 continue; 608 } 609 int j = 1; 610 int k = i + 1; 611 while (j < sublen) { 612 char source = Character.toLowerCase(src.charAt(k)); 613 if (sub.charAt(j) != source) { 614 continue mainloop; 615 } 616 j++; k++; 617 } 618 return i; 619 } 620 return -1; 621 } 622 623 624 635 public static int lastIndexOfIgnoreCase(String s, String subS) { 636 return lastIndexOfIgnoreCase(s, subS, s.length(), 0); 637 } 638 639 650 public static int lastIndexOfIgnoreCase(String src, String subS, int startIndex) { 651 return lastIndexOfIgnoreCase(src, subS, startIndex, 0); 652 } 653 663 public static int lastIndexOfIgnoreCase(String src, String sub, int startIndex, int endIndex) { 664 int sublen = sub.length(); 665 if (sublen == 0) { 666 return startIndex; 667 } 668 sub = sub.toLowerCase(); 669 int total = src.length() - sublen; 670 if (total < 0) { 671 return -1; 672 } 673 if (startIndex >= total) { 674 startIndex = total; 675 } 676 char c = sub.charAt(0); 677 mainloop: 678 for (int i = startIndex; i >= endIndex; i--) { 679 if (Character.toLowerCase(src.charAt(i)) != c) { 680 continue; 681 } 682 int j = 1; 683 int k = i + 1; 684 while (j < sublen) { 685 char source = Character.toLowerCase(src.charAt(k)); 686 if (sub.charAt(j) != source) { 687 continue mainloop; 688 } 689 j++; k++; 690 } 691 return i; 692 } 693 return -1; 694 } 695 696 706 public static int lastIndexOf(String src, String sub, int startIndex, int endIndex) { 707 int sublen = sub.length(); 708 if (sublen == 0) { 709 return startIndex; 710 } 711 int total = src.length() - sublen; 712 if (total < 0) { 713 return -1; 714 } 715 if (startIndex >= total) { 716 startIndex = total; 717 } 718 char c = sub.charAt(0); 719 mainloop: 720 for (int i = startIndex; i >= endIndex; i--) { 721 if (src.charAt(i) != c) { 722 continue; 723 } 724 int j = 1; 725 int k = i + 1; 726 while (j < sublen) { 727 if (sub.charAt(j) != src.charAt(k)) { 728 continue mainloop; 729 } 730 j++; k++; 731 } 732 return i; 733 } 734 return -1; 735 } 736 737 740 public static int lastIndexOf(String src, char c, int startIndex, int endIndex) { 741 int total = src.length() - 1; 742 if (total < 0) { 743 return -1; 744 } 745 if (startIndex >= total) { 746 startIndex = total; 747 } 748 for (int i = startIndex; i >= endIndex; i--) { 749 if (src.charAt(i) == c) { 750 return i; 751 } 752 } 753 return -1; 754 } 755 756 757 758 768 public static boolean startsWithIgnoreCase(String src, String subS) { 769 return startsWithIgnoreCase(src, subS, 0); 770 } 771 772 784 public static boolean startsWithIgnoreCase(String src, String subS, int startIndex) { 785 String sub = subS.toLowerCase(); 786 int sublen = sub.length(); 787 if (startIndex + sublen > src.length()) { 788 return false; 789 } 790 int j = 0; 791 int i = startIndex; 792 while (j < sublen) { 793 char source = Character.toLowerCase(src.charAt(i)); 794 if (sub.charAt(j) != source) { 795 return false; 796 } 797 j++; i++; 798 } 799 return true; 800 } 801 802 812 public static boolean endsWithIgnoreCase(String src, String subS) { 813 String sub = subS.toLowerCase(); 814 int sublen = sub.length(); 815 int j = 0; 816 int i = src.length() - sublen; 817 if (i < 0) { 818 return false; 819 } 820 while (j < sublen) { 821 char source = Character.toLowerCase(src.charAt(i)); 822 if (sub.charAt(j) != source) { 823 return false; 824 } 825 j++; i++; 826 } 827 return true; 828 } 829 830 831 833 840 public static int count(String source, String sub) { 841 return count(source, sub, 0); 842 } 843 public static int count(String source, String sub, int start) { 844 int count = 0; 845 int j = start; 846 while (true) { 847 int i = source.indexOf(sub, j); 848 if (i == -1) { 849 break; 850 } 851 count++; 852 j = i + sub.length(); 853 } 854 return count; 855 } 856 857 public static int count(String source, char c) { 858 |