| 1 16 package org.apache.commons.lang; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 116 public class StringUtils { 117 133 137 public static final String EMPTY = ""; 138 139 143 public static final int INDEX_NOT_FOUND = -1; 144 145 148 private static final int PAD_LIMIT = 8192; 149 150 155 private static final String [] PADDING = new String [Character.MAX_VALUE]; 156 157 static { 158 PADDING[32] = " "; 160 } 161 162 170 public StringUtils() { 171 } 173 174 194 public static boolean isEmpty(String str) { 195 return str == null || str.length() == 0; 196 } 197 198 212 public static boolean isNotEmpty(String str) { 213 return str != null && str.length() > 0; 214 } 215 216 231 public static boolean isBlank(String str) { 232 int strLen; 233 if (str == null || (strLen = str.length()) == 0) { 234 return true; 235 } 236 for (int i = 0; i < strLen; i++) { 237 if ((Character.isWhitespace(str.charAt(i)) == false)) { 238 return false; 239 } 240 } 241 return true; 242 } 243 244 260 public static boolean isNotBlank(String str) { 261 int strLen; 262 if (str == null || (strLen = str.length()) == 0) { 263 return false; 264 } 265 for (int i = 0; i < strLen; i++) { 266 if ((Character.isWhitespace(str.charAt(i)) == false)) { 267 return true; 268 } 269 } 270 return false; 271 } 272 273 294 public static String clean(String str) { 295 return str == null ? EMPTY : str.trim(); 296 } 297 298 321 public static String trim(String str) { 322 return str == null ? null : str.trim(); 323 } 324 325 347 public static String trimToNull(String str) { 348 String ts = trim(str); 349 return isEmpty(ts) ? null : ts; 350 } 351 352 373 public static String trimToEmpty(String str) { 374 return str == null ? EMPTY : str.trim(); 375 } 376 377 401 public static String strip(String str) { 402 return strip(str, null); 403 } 404 405 428 public static String stripToNull(String str) { 429 if (str == null) { 430 return null; 431 } 432 str = strip(str, null); 433 return str.length() == 0 ? null : str; 434 } 435 436 458 public static String stripToEmpty(String str) { 459 return str == null ? EMPTY : strip(str, null); 460 } 461 462 488 public static String strip(String str, String stripChars) { 489 if (isEmpty(str)) { 490 return str; 491 } 492 str = stripStart(str, stripChars); 493 return stripEnd(str, stripChars); 494 } 495 496 520 public static String stripStart(String str, String stripChars) { 521 int strLen; 522 if (str == null || (strLen = str.length()) == 0) { 523 return str; 524 } 525 int start = 0; 526 if (stripChars == null) { 527 while ((start != strLen) && Character.isWhitespace(str.charAt(start))) { 528 start++; 529 } 530 } else if (stripChars.length() == 0) { 531 return str; 532 } else { 533 while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) { 534 start++; 535 } 536 } 537 return str.substring(start); 538 } 539 540 564 public static String stripEnd(String str, String stripChars) { 565 int end; 566 if (str == null || (end = str.length()) == 0) { 567 return str; 568 } 569 570 if (stripChars == null) { 571 while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) { 572 end--; 573 } 574 } else if (stripChars.length() == 0) { 575 return str; 576 } else { 577 while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) { 578 end--; 579 } 580 } 581 return str.substring(0, end); 582 } 583 584 605 public static String [] stripAll(String [] strs) { 606 return stripAll(strs, null); 607 } 608 609 634 public static String [] stripAll(String [] strs, String stripChars) { 635 int strsLen; 636 if (strs == null || (strsLen = strs.length) == 0) { 637 return strs; 638 } 639 String [] newArr = new String [strsLen]; 640 for (int i = 0; i < strsLen; i++) { 641 newArr[i] = strip(strs[i], stripChars); 642 } 643 return newArr; 644 } 645 646 668 public static boolean equals(String str1, String str2) { 669 return str1 == null ? str2 == null : str1.equals(str2); 670 } 671 672 693 public static boolean equalsIgnoreCase(String str1, String str2) { 694 return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2); 695 } 696 697 718 public static int indexOf(String str, char searchChar) { 719 if (isEmpty(str)) { 720 return -1; 721 } 722 return str.indexOf(searchChar); 723 } 724 725 750 public static int indexOf(String str, char searchChar, int startPos) { 751 if (isEmpty(str)) { 752 return -1; 753 } 754 return str.indexOf(searchChar, startPos); 755 } 756 757 779 public static int indexOf(String str, String searchStr) { 780 if (str == null || searchStr == null) { 781 return -1; 782 } 783 return str.indexOf(searchStr); 784 } 785 786 813 public static int ordinalIndexOf(String str, String searchStr, int ordinal) { 814 if (str == null || searchStr == null || ordinal <= 0) { 815 return INDEX_NOT_FOUND; 816 } 817 if (searchStr.length() == 0) { 818 return 0; 819 } 820 int found = 0; 821 int index = INDEX_NOT_FOUND; 822 do { 823 index = str.indexOf(searchStr, index + 1); 824 if (index < 0) { 825 return index; 826 } 827 found++; 828 } while (found < ordinal); 829 return index; 830 } 831 832 863 public static int indexOf(String str, String searchStr, int startPos) { 864 if (str == null || searchStr == null) { 865 return -1; 866 } 867 if (searchStr.length() == 0 && startPos >= str.length()) { 869 return str.length(); 870 } 871 return str.indexOf(searchStr, startPos); 872 } 873 874 895 public static int lastIndexOf(String str, char searchChar) { 896 if (isEmpty(str)) { 897 return -1; 898 } 899 return str.lastIndexOf(searchChar); 900 } 901 902 929 public static int lastIndexOf(String str, char searchChar, int startPos) { 930 if (isEmpty(str)) { 931 return -1; 932 } 933 return str.lastIndexOf(searchChar, startPos); 934 } 935 936 958 public static int lastIndexOf(String str, String searchStr) { 959 if (str == null || searchStr == null) { 960 return -1; 961 } 962 return str.lastIndexOf(searchStr); 963 } 964 965 |