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 993 public static int lastIndexOf(String str, String searchStr, int startPos) { 994 if (str == null || searchStr == null) { 995 return -1; 996 } 997 return str.lastIndexOf(searchStr, startPos); 998 } 999 1000 1021 public static boolean contains(String str, char searchChar) { 1022 if (isEmpty(str)) { 1023 return false; 1024 } 1025 return str.indexOf(searchChar) >= 0; 1026 } 1027 1028 1049 public static boolean contains(String str, String searchStr) { 1050 if (str == null || searchStr == null) { 1051 return false; 1052 } 1053 return str.indexOf(searchStr) >= 0; 1054 } 1055 1056 1080 public static int indexOfAny(String str, char[] searchChars) { 1081 if (isEmpty(str) || ArrayUtils.isEmpty(searchChars)) { 1082 return -1; 1083 } 1084 for (int i = 0; i < str.length(); i++) { 1085 char ch = str.charAt(i); 1086 for (int j = 0; j < searchChars.length; j++) { 1087 if (searchChars[j] == ch) { 1088 return i; 1089 } 1090 } 1091 } 1092 return -1; 1093 } 1094 1095 1117 public static int indexOfAny(String str, String searchChars) { 1118 if (isEmpty(str) || isEmpty(searchChars)) { 1119 return -1; 1120 } 1121 return indexOfAny(str, searchChars.toCharArray()); 1122 } 1123 1124 1148 public static int indexOfAnyBut(String str, char[] searchChars) { 1149 if (isEmpty(str) || ArrayUtils.isEmpty(searchChars)) { 1150 return -1; 1151 } 1152 outer : for (int i = 0; i < str.length(); i++) { 1153 char ch = str.charAt(i); 1154 for (int j = 0; j < searchChars.length; j++) { 1155 if (searchChars[j] == ch) { 1156 continue outer; 1157 } 1158 } 1159 return i; 1160 } 1161 return -1; 1162 } 1163 1164 1186 public static int indexOfAnyBut(String str, String searchChars) { 1187 if (isEmpty(str) || isEmpty(searchChars)) { 1188 return -1; 1189 } 1190 for (int i = 0; i < str.length(); i++) { 1191 if (searchChars.indexOf(str.charAt(i)) < 0) { 1192 return i; 1193 } 1194 } 1195 return -1; 1196 } 1197 1198 1221 public static boolean containsOnly(String str, char[] valid) { 1222 if ((valid == null) || (str == null)) { 1224 return false; 1225 } 1226 if (str.length() == 0) { 1227 return true; 1228 } 1229 if (valid.length == 0) { 1230 return false; 1231 } 1232 return indexOfAnyBut(str, valid) == -1; 1233 } 1234 1235 1257 public static boolean containsOnly(String str, String validChars) { 1258 if (str == null || validChars == null) { 1259 return false; 1260 } 1261 return containsOnly(str, validChars.toCharArray()); 1262 } 1263 1264 1288 public static boolean containsNone(String str, char[] invalidChars) { 1289 if (str == null || invalidChars == null) { 1290 return true; 1291 } 1292 int strSize = str.length(); 1293 int validSize = invalidChars.length; 1294 for (int i = 0; i < strSize; i++) { 1295 char ch = str.charAt(i); 1296 for (int j = 0; j < validSize; j++) { 1297 if (invalidChars[j] == ch) { 1298 return false; 1299 } 1300 } 1301 } 1302 return true; 1303 } 1304 1305 1327 public static boolean containsNone(String str, String invalidChars) { 1328 if (str == null || invalidChars == null) { 1329 return true; 1330 } 1331 return containsNone(str, invalidChars.toCharArray()); 1332 } 1333 1334 1362 public static int indexOfAny(String str, String [] searchStrs) { 1363 if ((str == null) || (searchStrs == null)) { 1364 return -1; 1365 } 1366 int sz = searchStrs.length; 1367 1368 int ret = Integer.MAX_VALUE; 1370 1371 int tmp = 0; 1372 for (int i = 0; i < sz; i++) { 1373 String search = searchStrs[i]; 1374 if (search == null) { 1375 continue; 1376 } 1377 tmp = str.indexOf(search); 1378 if (tmp == -1) { 1379 continue; 1380 } 1381 1382 if (tmp < ret) { 1383 ret = tmp; 1384 } 1385 } 1386 1387 return (ret == Integer.MAX_VALUE) ? -1 : ret; 1388 } 1389 1390 1415 public static int lastIndexOfAny(String str, String [] searchStrs) { 1416 if ((str == null) || (searchStrs == null)) { 1417 return -1; 1418 } 1419 int sz = searchStrs.length; 1420 int ret = -1; 1421 int tmp = 0; 1422 for (int i = 0; i < sz; i++) { 1423 String search = searchStrs[i]; 1424 if (search == null) { 1425 continue; 1426 } 1427 tmp = str.lastIndexOf(search); 1428 if (tmp > ret) { 1429 ret = tmp; 1430 } 1431 } 1432 return ret; 1433 } 1434 1435 1461 public static String substring(String str, int start) { 1462 if (str == null) { 1463 return null; 1464 } 1465 1466 if (start < 0) { 1468 start = str.length() + start; } 1470 1471 if (start < 0) { 1472 start = 0; 1473 } 1474 if (start > str.length()) { 1475 return EMPTY; 1476 } 1477 1478 return str.substring(start); 1479 } 1480 1481 1516 public static String substring(String str, int start, int end) { 1517 if (str == null) { 1518 return null; 1519 } 1520 1521 if (end < 0) { 1523 end = str.length() + end; } 1525 if (start < 0) { 1526 start = str.length() + start; } 1528 1529 if (end > str.length()) { 1531 end = str.length(); 1532 } 1533 1534 if (start > end) { 1536 return EMPTY; 1537 } 1538 1539 if (start < 0) { 1540 start = 0; 1541 } 1542 if (end < 0) { 1543 end = 0; 1544 } 1545 1546 return str.substring(start, end); 1547 } 1548 1549 1571 public static String left(String str, int len) { 1572 if (str == null) { 1573 return null; 1574 } 1575 if (len < 0) { 1576 return EMPTY; 1577 } 1578 if (str.length() <= len) { 1579 return str; 1580 } else { 1581 return str.substring(0, len); 1582 } 1583 } 1584 1585 1605 public static String right(String str, int len) { 1606 if (str == null) { 1607 return null; 1608 } 1609 if (len < 0) { 1610 return EMPTY; 1611 } 1612 if (str.length() <= len) { 1613 return str; 1614 } else { 1615 return str.substring(str.length() - len); 1616 } 1617 } 1618 1619 1643 public static String mid(String str, int pos, int len) { 1644 if (str == null) { 1645 return null; 1646 } 1647 if (len < 0 || pos > str.length()) { 1648 return EMPTY; 1649 } 1650 if (pos < 0) { 1651 pos = 0; 1652 } 1653 if (str.length() <= (pos + len)) { 1654 return str.substring(pos); 1655 } else { 1656 return str.substring(pos, pos + len); 1657 } 1658 } 1659 1660 1687 public static String substringBefore(String str, String separator) { 1688 if (isEmpty(str) || separator == null) { 1689 return str; 1690 } 1691 if (separator.length() == 0) { 1692 return EMPTY; 1693 } 1694 int pos = str.indexOf(separator); 1695 if (pos == -1) { 1696 return str; 1697 } 1698 return str.substring(0, pos); 1699 } 1700 1701 1727 public static String substringAfter(String str, String separator) { 1728 if (isEmpty(str)) { 1729 return str; 1730 } 1731 if (separator == null) { 1732 return EMPTY; 1733 } 1734 int pos = str.indexOf(separator); 1735 if (pos == -1) { 1736 return EMPTY; 1737 } 1738 return str.substring(pos + separator.length()); 1739 } 1740 1741 1766 public static String substringBeforeLast(String str, String separator) { 1767 if (isEmpty(str) || isEmpty(separator)) { 1768 return str; 1769 } 1770 int pos = str.lastIndexOf(separator); 1771 if (pos == -1) { 1772 return str; 1773 } 1774 return str.substring(0, pos); 1775 } 1776 1777 1804 public static String substringAfterLast(String str, String separator) { 1805 if (isEmpty(str)) { 1806 return str; 1807 } 1808 if (isEmpty(separator)) { 1809 return EMPTY; 1810 } 1811 int pos = str.lastIndexOf(separator); 1812 if (pos == -1 || pos == (str.length() - separator.length())) { 1813 return EMPTY; 1814 } 1815 return str.substring(pos + separator.length()); 1816 } 1817 1818 1841 public static String substringBetween(String str, String tag) { 1842 return substringBetween(str, tag, tag); 1843 } 1844 1845 1870 public static String substringBetween(String str, String open, String close) { 1871 if (str == null || open == null || close == null) { 1872 return null; 1873 } 1874 int start = str.indexOf(open); 1875 if (start != -1) { 1876 int end = str.indexOf(close, start + open.length()); 1877 if (end != -1) { 1878 return str.substring(start + open.length(), end); 1879 } 1880 } 1881 return null; 1882 } 1883 1884 1908 public static String getNestedString(String str, String tag) { 1909 return substringBetween(str, tag, tag); 1910 } 1911 1912 1938 public static String getNestedString(String str, String open, String close) { 1939 return substringBetween(str, open, close); 1940 } 1941 1942 1966 public static String [] split(String str) { 1967 return split(str, null, -1); 1968 } 1969 1970 1996 public static String [] split(String str, char separatorChar) { 1997 return splitWorker(str, separatorChar, false); 1998 } 1999 2000 2025 public static String [] split(String str, String separatorChars) { 2026 return splitWorker(str, separatorChars, -1, false); 2027 } 2028 2029 2059 public static String [] split(String str, String separatorChars, int max) { 2060 return splitWorker(str, separatorChars, max, false); 2061 } 2062 2063 2087 public static String [] splitByWholeSeparator(String str, String separator) { 2088 return splitByWholeSeparator( str, separator, -1 ) ; 2089 } 2090 2091 2118 public static String [] splitByWholeSeparator( String str, String separator, int max ) { 2119 if (str == null) { 2120 return null; 2121 } 2122 2123 int len = str.length() ; 2124 2125 if (len == 0) { 2126 return ArrayUtils.EMPTY_STRING_ARRAY; 2127 } 2128 2129 if ( ( separator == null ) || ( "".equals( separator ) ) ) { 2130 return split( str, null, max ) ; 2132 } 2133 2134 2135 int separatorLength = separator.length() ; 2136 2137 ArrayList substrings = new ArrayList () ; 2138 int numberOfSubstrings = 0 ; 2139 int beg = 0 ; 2140 int end = 0 ; 2141 while ( end < len ) { 2142 end = str.indexOf( separator, beg ) ; 2143 2144 if ( end > -1 ) { 2145 if ( end > beg ) { 2146 numberOfSubstrings += 1 ; 2147 2148 if ( numberOfSubstrings == max ) { 2149 end = len ; 2150 substrings.add( str.substring( beg ) ) ; 2151 } else { 2152 substrings.add( str.substring( beg, end ) ) ; 2155 2156 beg = end + separatorLength ; 2160 } 2161 } else { 2162 beg = end + separatorLength ; 2164 } 2165 } else { 2166 substrings.add( str.substring( beg ) ) ; 2168 end = len ; 2169 } 2170 } 2171 2172 return (String []) substrings.toArray( new String [substrings.size()] ) ; 2173 } 2174 2175 2176 2201 public static String [] splitPreserveAllTokens(String str) { 2202 return splitWorker(str, null, -1, true); 2203 } 2204 2205 2237 public static String [] splitPreserveAllTokens(String str, char separatorChar) { 2238 return splitWorker(str, separatorChar, true); 2239 } 2240 2241 2253 private static String [] splitWorker(String str, char separatorChar, boolean preserveAllTokens) { 2254 2256 if (str == null) { 2257 return null; 2258 } 2259 int len = str.length(); 2260 if (len == 0) { 2261 return ArrayUtils.EMPTY_STRING_ARRAY; 2262 } 2263 List list = new ArrayList (); 2264 int i = 0, start = 0; 2265 boolean match = false; 2266 boolean lastMatch = false; 2267 while (i < len) { 2268 if (str.charAt(i) == separatorChar) { 2269 if (match || preserveAllTokens) { 2270 list.add(str.substring(start, i)); 2271 match = false; 2272 lastMatch = true; 2273 } 2274 start = ++i; 2275 continue; 2276 } else { 2277 lastMatch = false; 2278 } 2279 match = true; 2280 i++; 2281 } 2282 if (match || (preserveAllTokens && lastMatch)) { 2283 list.add(str.substring(start, i)); 2284 } 2285 return (String []) list.toArray(new String [list.size()]); 2286 } 2287 2288 2321 public static String [] splitPreserveAllTokens(String str, String separatorChars) { 2322 return splitWorker(str, separatorChars, -1, true); 2323 } 2324 2325 2361 public static String [] splitPreserveAllTokens(String str, String separatorChars, int max) { 2362 return splitWorker(str, separatorChars, max, true); 2363 } 2364 2365 2379 private static String [] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { 2380 2384 if (str == null) { 2385 return null; 2386 } 2387 int len = str.length(); 2388 if (len == 0) { 2389 return ArrayUtils.EMPTY_STRING_ARRAY; 2390 } 2391 List list = new ArrayList (); 2392 int sizePlus1 = 1; 2393 int i = 0, start = 0; 2394 boolean match = false; 2395 boolean lastMatch = false; 2396 if (separatorChars == null) { 2397 while (i < len) { 2399 if (Character.isWhitespace(str.charAt(i))) { 2400 if (match || preserveAllTokens) { 2401 lastMatch = true; 2402 if (sizePlus1++ == max) { 2403 i = len; 2404 lastMatch = false; 2405 } 2406 list.add(str.substring(start, i)); 2407 match = false; 2408 } 2409 start = ++i; 2410 continue; 2411 } else { 2412 lastMatch = false; 2413 } 2414 match = true; 2415 i++; 2416 } 2417 } else if (separatorChars.length() == 1) { 2418 char sep = separatorChars.charAt(0); 2420 while (i < len) { 2421 if (str.charAt(i) == sep) { 2422 if (match || preserveAllTokens) { 2423 lastMatch = true; 2424 if (sizePlus1++ == max) { 2425 i = len; 2426 lastMatch = false; 2427 } 2428 list.add(str.substring(start, i)); 2429 match = false; 2430 } 2431 start = ++i; 2432 continue; 2433 } else { 2434 lastMatch = false; 2435 } 2436 match = true; 2437 i++; 2438 } 2439 } else { 2440 while (i < len) { 2442 if (separatorChars.indexOf(str.charAt(i)) >= 0) { 2443 if (match || preserveAllTokens) { 2444 lastMatch = true; 2445 if (sizePlus1++ == max) { 2446 i = len; 2447 lastMatch = false; 2448 } 2449 list.add(str.substring(start, i)); 2450 match = false; 2451 } 2452 start = ++i; 2453 continue; 2454 } else { 2455 lastMatch = false; 2456 } 2457 match = true; 2458 i++; 2459 } 2460 } 2461 if (match || (preserveAllTokens && lastMatch)) { 2462 list.add(str.substring(start, i)); 2463 } 2464 return (String []) list.toArray(new String [list.size()]); 2465 } 2466 2467 2487 public static String concatenate(Object [] array) { 2488 return join(array, null); 2489 } 2490 2491 2511 public static String join(Object [] array) { 2512 return join(array, null); 2513 } 2514 2515 2537 public static String join(Object [] array, char separator) { 2538 if (array == null) { 2539 return null; 2540 } 2541 int arraySize = array.length; 2542 int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize); 2543 StringBuffer buf = new StringBuffer (bufSize); 2544 2545 for (int i = 0; i < arraySize; i++) { 2546 if (i > 0) { 2547 buf.append(separator); 2548 } 2549 if (array[i] != null) { 2550 buf.append(array[i]); 2551 } 2552 } 2553 return buf.toString(); 2554 } 2555 2556 2579 public static String join(Object [] array, String separator) { 2580 if (array == null) { 2581 return null; 2582 } 2583 if (separator == null) { 2584 separator = EMPTY; 2585 } 2586 int arraySize = array.length; 2587 2588 int bufSize = 2592 ((arraySize == 0) 2593 ? 0 2594 : arraySize 2595 * ((array[0] == null ? 16 : array[0].toString().length()) 2596 + separator.length())); 2597 2598 StringBuffer buf = new StringBuffer (bufSize); 2599 2600 for (int i = 0; i < arraySize; i++) { 2601 if (i > 0) { 2602 buf.append(separator); 2603 } 2604 if (array[i] != null) { 2605 buf.append(array[i]); 2606 } 2607 } 2608 return buf.toString(); 2609 } 2610 2611 2625 public static String join(Iterator iterator, char separator) { 2626 if (iterator == null) { 2627 return null; 2628 } 2629 StringBuffer buf = new StringBuffer (256); while (iterator.hasNext()) { 2631 Object obj = iterator.next(); 2632 if (obj != null) { 2633 buf.append(obj); 2634 } 2635 if (iterator.hasNext()) { 2636 buf.append(separator); 2637 } 2638 } 2639 return buf.toString(); 2640 } 2641 2642 2655 public static String join(Iterator iterator, String separator) { 2656 if (iterator == null) { 2657 return null; 2658 } 2659 StringBuffer buf = new StringBuffer (256); while (iterator.hasNext()) { 2661 Object obj = iterator.next(); 2662 if (obj != null) { 2663 buf.append(obj); 2664 } 2665 if ((separator != null) && iterator.hasNext()) { 2666 buf.append(separator); 2667 } 2668 } 2669 return buf.toString(); 2670 } 2671 2672 2700 public static String deleteSpaces(String str) { 2701 if (str == null) { 2702 return null; 2703 } 2704 return CharSetUtils.delete(str, " \t\r\n\b"); 2705 } 2706 2707 2721 public static String deleteWhitespace(String str) { 2722 if (isEmpty(str)) { 2723 return str; 2724 } 2725 int sz = str.length(); 2726 char[] chs = new char[sz]; 2727 int count = 0; 2728 for (int i = 0; i < sz; i++) { 2729 if (!Character.isWhitespace(str.charAt(i))) { 2730 chs[count++] = str.charAt(i); 2731 } 2732 } 2733 if (count == sz) { 2734 return str; 2735 } 2736 return new String (chs, 0, count); 2737 } 2738 2739 2765 public static String removeStart(String str, String remove) { 2766 if (isEmpty(str) || isEmpty(remove)) { 2767 return str; 2768 } 2769 if (str.startsWith(remove)){ 2770 return str.substring(remove.length()); 2771 } 2772 return str; 2773 } 2774 2775 2799 public static String removeEnd(String str, String remove) { 2800 if (isEmpty(str) || isEmpty(remove)) { 2801 return str; 2802 } 2803 if (str.endsWith(remove)) { 2804 return str.substring(0, str.length() - remove.length()); 2805 } 2806 return str; 2807 } 2808 2809 2832 public static String remove(String str, String remove) { 2833 if (isEmpty(str) || isEmpty(remove)) { 2834 return str; 2835 } 2836 return replace(str, remove, "", -1); 2837 } 2838 2839 2858 public static String remove(String str, char remove) { 2859 if (isEmpty(str) || str.indexOf(remove) == -1) { 2860 return str; 2861 } 2862 char[] chars = str.toCharArray(); 2863 int pos = 0; 2864 for (int i = 0; i < chars.length; i++) { 2865 if (chars[i] != remove) { 2866 chars[pos++] = chars[i]; 2867 } 2868 } 2869 return new String (chars, 0, pos); 2870 } 2871 2872 2897 public static String replaceOnce(String text, String repl, String with) { 2898 return replace(text, repl, with, 1); 2899 } 2900 2901 2924 public static String replace(String text, String repl, String with) { 2925 return replace(text, repl, with, -1); 2926 } 2927 2928 2956 public static String replace(String text, String repl, String with, int max) { 2957 if (text == null || isEmpty(repl) || with == null || max == 0) { 2958 return text; 2959 } 2960 2961 StringBuffer buf = new StringBuffer (text.length()); 2962 int start = 0, end = 0; 2963 while ((end = text.indexOf(repl, start)) != -1) { 2964 buf.append(text.substring(start, end)).append(with); 2965 start = end + repl.length(); 2966 2967 if (--max == 0) { 2968 break; 2969 } 2970 } 2971 buf.append(text.substring(start)); 2972 return buf.toString(); 2973 } 2974 2975 2997 public static String replaceChars(String str, char searchChar, char replaceChar) { 2998 if (str == null) { 2999 return null; 3000 } 3001 return str.replace(searchChar, replaceChar); 3002 } 3003 3004 3040 public static String replaceChars(String str, String searchChars, String replaceChars) { 3041 if (isEmpty(str) || isEmpty(searchChars)) { 3042 return str; 3043 } 3044 if (replaceChars == null) { 3045 replaceChars = ""; 3046 } 3047 boolean modified = false; 3048 StringBuffer buf = new StringBuffer (str.length()); 3049 for (int i = 0; i < str.length(); i++) { 3050 char ch = str.charAt(i); 3051 int index = searchChars.indexOf(ch); 3052 if (index >= 0) { 3053 modified = true; 3054 if (index < replaceChars.length()) { 3055 buf.append(replaceChars.charAt(index)); 3056 } 3057 } else { 3058 buf.append(ch); 3059 } 3060 } 3061 if (modified) { 3062 return buf.toString(); 3063 } else { 3064 return str; 3065 } 3066 } 3067 3068 3095 public static String overlayString(String text, String overlay, int start, int end) { 3096 return new StringBuffer (start + overlay.length() + text.length() - end + 1) 3097 .append(text.substring(0, start)) 3098 .append(overlay) 3099 .append(text.substring(end)) 3100 .toString(); 3101 } 3102 3103 3132 public static String overlay(String str, String overlay, int start, int end) { 3133 if (str == null) { 3134 return null; 3135 } 3136 if (overlay == null) { 3137 overlay = EMPTY; 3138 } 3139 int len = str.length(); 3140 if (start < 0) { 3141 start = 0; 3142 } 3143 if (start > len) { 3144 start = len; 3145 } 3146 if (end < 0) { 3147 end = 0; 3148 } 3149 if (end > len) { 3150 end = len; 3151 } 3152 if (start > end) { 3153 int temp = start; 3154 start = end; 3155 end = temp; 3156 } 3157 return new StringBuffer (len + start - end + overlay.length() + 1) 3158 .append(str.substring(0, start)) 3159 .append(overlay) 3160 .append(str.substring(end)) 3161 .toString(); 3162 } 3163 3164 3191 public static String chomp(String str) { 3192 if (isEmpty(str)) { 3193 return str; 3194 } 3195 3196 if (str.length() == 1) { 3197 char ch = str.charAt(0); 3198 if (ch == '\r' || ch == '\n') { 3199 return EMPTY; 3200 } else { 3201 return str; 3202 } 3203 } 3204 3205 int lastIdx = str.length() - 1; 3206 char last = str.charAt(lastIdx); 3207 3208 if (last == '\n') { 3209 if (str.charAt(lastIdx - 1) == '\r') { 3210 lastIdx--; 3211 } 3212 } else if (last == '\r') { 3213 } else { 3216 lastIdx++; 3217 } 3218 return str.substring(0, lastIdx); 3219 } 3220 3221 3247 public static String chomp(String str, String separator) { 3248 if (isEmpty(str) || separator == null) { 3249 return str; 3250 } 3251 if (str.endsWith(separator)) { 3252 return str.substring(0, str.length() - separator.length()); 3253 } 3254 return str; 3255 } 3256 3257 3267 public static String chompLast(String str) { 3268 return chompLast(str, "\n"); 3269 } 3270 3271 3281 public static String chompLast(String str, String sep) { 3282 if (str.length() == 0) { 3283 return str; 3284 } 3285 String sub = str.substring(str.length() - sep.length()); 3286 if (sep.equals(sub)) { 3287 return str.substring(0, str.length() - sep.length()); 3288 } else { 3289 return str; 3290 } 3291 } 3292 3293 3305 public static String getChomp(String str, String sep) { 3306 int idx = str.lastIndexOf(sep); 3307 if (idx == str.length() - sep.length()) { 3308 return sep; 3309 } else if (idx != -1) { 3310 return str.substring(idx); 3311 } else { 3312 return EMPTY; 3313 } 3314 } 3315 3316 3327 public static String prechomp(String str, String sep) { 3328 int idx = str.indexOf(sep); 3329 if (idx != -1) { 3330 return str.substring(idx + sep.length()); 3331 } else { 3332 return str; 3333 } 3334 } 3335 3336 3348 public static String getPrechomp(String str, String sep) { 3349 int idx = str.indexOf(sep); 3350 if (idx != -1) { 3351 return str.substring(0, idx + sep.length()); 3352 } else { 3353 return EMPTY; 3354 } 3355 } 3356 3357 3382 public static String chop(String str) { 3383 if (str == null) { 3384 return null; 3385 } 3386 int strLen = str.length(); 3387 if (strLen < 2) { 3388 return EMPTY; 3389 } 3390 int lastIdx = strLen - 1; 3391 String ret = str.substring(0, lastIdx); 3392 char last = str.charAt(lastIdx); 3393 if (last == '\n') { 3394 if (ret.charAt(lastIdx - 1) == '\r') { 3395 return ret.substring(0, lastIdx - 1); 3396 } 3397 } 3398 return ret; 3399 } 3400 3401 3411 public static String chopNewline(String str) { 3412 int lastIdx = str.length() - 1; 3413 if (lastIdx <= 0) { 3414 return EMPTY; 3415 } 3416 char last = str.charAt(lastIdx); 3417 if (last == '\n') { 3418 if (str.charAt(lastIdx - 1) == '\r') { 3419 lastIdx--; 3420 } 3421 } else { 3422 lastIdx++; 3423 } 3424 return str.substring(0, lastIdx); 3425 } 3426 3427 3445 public static String escape(String str) { 3446 return StringEscapeUtils.escapeJava(str); 3447 } 3448 3449 3469 public static String repeat(String str, int repeat) { 3470 3472 if (str == null) { 3473 return null; 3474 } 3475 if (repeat <= 0) { 3476 return EMPTY; 3477 } 3478 int inputLength = str.length(); 3479 if (repeat == 1 || inputLength == 0) { 3480 return str; 3481 } 3482 if (inputLength == 1 && repeat <= PAD_LIMIT) { 3483 return padding(repeat, str.charAt(0)); 3484 } 3485 3486 int outputLength = inputLength * repeat; 3487 switch (inputLength) { 3488 case 1 : 3489 char ch = str.charAt(0); 3490 char[] output1 = new char[outputLength]; 3491 for (int i = repeat - 1; i >= 0; i--) { 3492 output1[i] = ch; 3493 } 3494 return new String (output1); 3495 case 2 : 3496 char ch0 = str.charAt(0); 3497 char ch1 = str.charAt(1); 3498 char[] output2 = new char[outputLength]; 3499 for (int i = repeat * 2 - 2; i >= 0; i--, i--) { 3500 output2[i] = ch0; 3501 output2[i + 1] = ch1; 3502 } 3503 return new String (output2); 3504 default : 3505 StringBuffer buf = new StringBuffer (outputLength); 3506 for (int i = 0; i < repeat; i++) { 3507 buf.append(str); 3508 } 3509 return buf.toString(); 3510 } 3511 } 3512 3513 3528 private static String padding(int repeat, char padChar) { 3529 String pad = PADDING[padChar]; 3532 if (pad == null) { 3533 pad = String.valueOf(padChar); 3534 } 3535 while (pad.length() < repeat) { 3536 pad = pad.concat(pad); 3537 } 3538 PADDING[padChar] = pad; 3539 return pad.substring(0, repeat); 3540 } 3541 3542 3561 public static String rightPad(String str, int size) { 3562 return rightPad(str, size, ' '); 3563 } 3564 3565 3586 public static String rightPad(String str, int size, char padChar) { 3587 if (str == null) { 3588 return null; 3589 } 3590 int pads = size - str.length(); 3591 if (pads <= 0) { 3592 return str; } 3594 if (pads > PAD_LIMIT) { 3595 return rightPad(str, size, String.valueOf(padChar)); 3596 } 3597 return str.concat(padding(pads, padChar)); 3598 } 3599 3600 3623 public static String rightPad(String str, int size, String padStr) { 3624 if (str == null) { 3625 return null; 3626 } 3627 if (isEmpty(padStr)) { 3628 padStr = " "; 3629 } 3630 int padLen = padStr.length(); 3631 int strLen = str.length(); 3632 int pads = size - strLen; 3633 if (pads <= 0) { 3634 return str; } 3636 if (padLen == 1 && pads <= PAD_LIMIT) { 3637 return rightPad(str, size, padStr.charAt(0)); 3638 } 3639 3640 if (pads == padLen) { 3641 return str.concat(padStr); 3642 } else if (pads < padLen) { 3643 return str.concat(padStr.substring(0, pads)); 3644 } else { 3645 char[] padding = new char[pads]; 3646 char[] padChars = padStr.toCharArray(); 3647 for (int i = 0; i < pads; i++) { 3648 padding[i] = padChars[i % padLen]; 3649 } 3650 return str.concat(new String (padding)); 3651 } 3652 } 3653 3654 3673 public static String leftPad(String str, int size) { 3674 return leftPad(str, size, ' '); 3675 } 3676 3677 3698 public static String leftPad(String str, int size, char padChar) { 3699 if (str == null) { 3700 return null; 3701 } 3702 int pads = size - str.length(); 3703 if (pads <= 0) { 3704 return str; } 3706 if (pads > PAD_LIMIT) { 3707 return leftPad(str, size, String.valueOf(padChar)); 3708 } 3709 return padding(pads, padChar).concat(str); 3710 } 3711 3712 3735 public static String leftPad(String str, int size, String padStr) { 3736 if (str == null) { 3737 return null; 3738 } 3739 if (isEmpty(padStr)) { 3740 padStr = " "; 3741 } 3742 int padLen = padStr.length(); 3743 int strLen = str.length(); 3744 int pads = size - strLen; 3745 if (pads <= 0) { 3746 return str; } 3748 if (padLen == 1 && pads <= PAD_LIMIT) { 3749 return leftPad(str, size, padStr.charAt(0)); 3750 } 3751 3752 if (pads == padLen) { 3753 return padStr.concat(str); 3754 } else if (pads < padLen) { 3755 return padStr.substring(0, pads).concat(str); 3756 } else { 3757 char[] padding = new char[pads]; 3758 char[] padChars = padStr.toCharArray(); 3759 for (int i = 0; i < pads; i++) { 3760 padding[i] = padChars[i % padLen]; 3761 } 3762 return new String (padding).concat(str); 3763 } 3764 } 3765 3766 3791 public static String center(String str, int size) { 3792 return center(str, size, ' '); 3793 } 3794 3795 3819 public static String center(String str, int size, char padChar) { 3820 if (str == null || size <= 0) { 3821 return str; 3822 } 3823 int strLen = str.length(); 3824 int pads = size - strLen; 3825 if (pads <= 0) { 3826 return str; 3827 } 3828 str = leftPad(str, strLen + pads / 2, padChar); 3829 str = rightPad(str, size, padChar); 3830 return str; 3831 } 3832 3833 3859 public static String center(String str, int size, String padStr) { 3860 if (str == null || size <= 0) { 3861 return str; 3862 } 3863 if (isEmpty(padStr)) { 3864 padStr = " "; 3865 } 3866 int strLen = str.length(); 3867 int pads = size - strLen; 3868 if (pads <= 0) { 3869 return str; 3870 } 3871 str = leftPad(str, strLen + pads / 2, padStr); 3872 str = rightPad(str, size, padStr); 3873 return str; 3874 } 3875 3876 3892 public static String upperCase(String str) { 3893 if (str == null) { 3894 return null; 3895 } 3896 return str.toUpperCase(); 3897 } 3898 3899 3913 public static String lowerCase(String str) { 3914 if (str == null) { 3915 return null; 3916 } 3917 return str.toLowerCase(); 3918 } 3919 3920 3940 public static String capitalize(String str) { 3941 int strLen; 3942 if (str == null || (strLen = str.length()) == 0) { 3943 return str; 3944 } 3945 return new StringBuffer (strLen) 3946 .append(Character.toTitleCase(str.charAt(0))) 3947 .append(str.substring(1)) 3948 .toString(); 3949 } 3950 3951 3960 public static String capitalise(String str) { 3961 return capitalize(str); 3962 } 3963 3964 3984 public static String uncapitalize(String str) { 3985 int strLen; 3986 if (str == null || (strLen = str.length()) == 0) { 3987 return str; 3988 } 3989 return new StringBuffer (strLen) 3990 .append(Character.toLowerCase(str.charAt(0))) 3991 .append(str.substring(1)) 3992 .toString(); 3993 } 3994 3995 4004 public static String uncapitalise(String str) { 4005 return uncapitalize(str); 4006 } 4007 4008 4035 public static String swapCase(String str) { 4036 int strLen; 4037 if (str == null || (strLen = str.length()) == 0) { 4038 return str; 4039 } 4040 StringBuffer buffer = new StringBuffer (strLen); 4041 4042 char ch = 0; 4043 for (int i = 0; i < strLen; i++) { 4044 ch = str.charAt(i); 4045 if (Character.isUpperCase(ch)) { 4046 ch = Character.toLowerCase(ch); 4047 } else if (Character.isTitleCase(ch)) { 4048 ch = Character.toLowerCase(ch); 4049 } else if (Character.isLowerCase(ch)) { 4050 ch = Character.toUpperCase(ch); 4051 } 4052 buffer.append(ch); 4053 } 4054 return buffer.toString(); 4055 } 4056 4057 4069 public static String capitaliseAllWords(String str) { 4070 return WordUtils.capitalize(str); 4071 } 4072 4073 4094 public static int countMatches(String str, String sub) { 4095 if (isEmpty(str) || isEmpty(sub)) { 4096 return 0; 4097 } 4098 int count = 0; 4099 int idx = 0; 4100 while ((idx = str.indexOf(sub, idx)) != -1) { 4101 count++; 4102 idx += sub.length(); 4103 } 4104 return count; 4105 } 4106 4107 4127 public static boolean isAlpha(String str) { 4128 if (str == null) { 4129 return false; 4130 } 4131 int sz = str.length(); 4132 for (int i = 0; i < sz; i++) { 4133 if (Character.isLetter(str.charAt(i)) == false) { 4134 return false; 4135 } 4136 } 4137 return true; 4138 } 4139 4140 4161 public static boolean isAlphaSpace(String str) { 4162 if (str == null) { 4163 return false; 4164 } 4165 int sz = str.length(); 4166 for (int i = 0; i < sz; i++) { 4167 if ((Character.isLetter(str.charAt(i)) == false) && (str.charAt(i) != ' ')) { 4168 return false; 4169 } 4170 } 4171 return true; 4172 } 4173 4174 4194 public static boolean isAlphanumeric(String str) { 4195 if (str == null) { 4196 return false; 4197 } 4198 int sz = str.length(); 4199 for (int i = 0; i < sz; i++) { 4200 if (Character.isLetterOrDigit(str.charAt(i)) == false) { 4201 return false; 4202 } 4203 } 4204 return true; 4205 } 4206 4207 4228 public static boolean isAlphanumericSpace(String str) { 4229 if (str == null) { 4230 return false; 4231 } 4232 int sz = str.length(); 4233 for (int i = 0; i < sz; i++) { 4234 if ((Character.isLetterOrDigit(str.charAt(i)) == false) && (str.charAt(i) != ' ')) { 4235 return false; 4236 } 4237 } 4238 return true; 4239 } 4240 4241 4266 public static boolean isAsciiPrintable(String str) { 4267 if (str == null) { 4268 return false; 4269 } 4270 int sz = str.length(); 4271 for (int i = 0; i < sz; i++) { 4272 if (CharUtils.isAsciiPrintable(str.charAt(i)) == false) { 4273 return false; 4274 } 4275 } 4276 return true; 4277 } 4278 4279 4300 public static boolean isNumeric(String str) { 4301 if (str == null) { 4302 return false; 4303 } 4304 int sz = str.length(); 4305 for (int i = 0; i < sz; i++) { 4306 if (Character.isDigit(str.charAt(i)) == false) { 4307 return false; 4308 } 4309 } 4310 return true; 4311 } 4312 4313 4336 public static boolean isNumericSpace(String str) { 4337 if (str == null) { 4338 return false; 4339 } 4340 int sz = str.length(); 4341 for (int i = 0; i < sz; i++) { 4342 if ((Character.isDigit(str.charAt(i)) == false) && (str.charAt(i) != ' ')) { 4343 return false; 4344 } 4345 } 4346 return true; 4347 } 4348 4349 4368 public static boolean isWhitespace(String str) { 4369 if (str == null) { 4370 return false; 4371 } 4372 int sz = str.length(); 4373 for (int i = 0; i < sz; i++) { 4374 if ((Character.isWhitespace(str.charAt(i)) == false)) { 4375 return false; 4376 } 4377 } 4378 return true; 4379 } 4380 4381 4399 public static String defaultString(String str) { 4400 return str == null ? EMPTY : str; 4401 } 4402 4403 4420 public static String defaultString(String str, String defaultStr) { 4421 return str == null ? defaultStr : str; 4422 } 4423 4424 4440 public static String defaultIfEmpty(String str, String defaultStr) { 4441 return StringUtils.isEmpty(str) ? defaultStr : str; 4442 } 4443 4444 4460 public static String reverse(String str) { 4461 if (str == null) { 4462 return null; 4463 } 4464 return new StringBuffer (str).reverse().toString(); 4465 } 4466 4467 4486 public static String reverseDelimited(String str, char separatorChar) { 4487 if (str == null) { 4488 return null; 4489 } 4490 String [] strs = split(str, separatorChar); 4493 ArrayUtils.reverse(strs); 4494 return join(strs, separatorChar); 4495 } 4496 4497 4519 public static String reverseDelimitedString(String str, String separatorChars) { 4520 if (str == null) { 4521 return null; 4522 } 4523 String [] strs = split(str, separatorChars); 4526 ArrayUtils.reverse(strs); 4527 if (separatorChars == null) { 4528 return join(strs, ' '); 4529 } 4530 return join(strs, separatorChars); 4531 } 4532 4533 4567 public static String abbreviate(String str, int maxWidth) { 4568 return abbreviate(str, 0, maxWidth); 4569 } 4570 4571 4606 public static String abbreviate(String str, int offset, int maxWidth) { 4607 if (str == null) { 4608 return null; 4609 } 4610 if (maxWidth < 4) { 4611 throw new IllegalArgumentException ("Minimum abbreviation width is 4"); 4612 } 4613 if (str.length() <= maxWidth) { 4614 return str; 4615 } 4616 if (offset > str.length()) { 4617 offset = str.length(); 4618 } 4619 if ((str.length() - offset) < (maxWidth - 3)) { 4620 offset = str.length() - (maxWidth - 3); 4621 } 4622 if (offset <= 4) { 4623 return str.substring(0, maxWidth - 3) + "..."; 4624 } 4625 if (maxWidth < 7) { 4626 throw new IllegalArgumentException ("Minimum abbreviation width with offset is 7"); 4627 } 4628 if ((offset + (maxWidth - 3)) < str.length()) { 4629 return "..." + abbreviate(str.substring(offset), maxWidth - 3); 4630 } 4631 return "..." + str.substring(str.length() - (maxWidth - 3)); 4632 } 4633 4634 4661 public static String difference(String str1, String str2) { 4662 if (str1 == null) { 4663 return str2; 4664 } 4665 if (str2 == null) { 4666 return str1; 4667 } 4668 int at = indexOfDifference(str1, str2); 4669 if (at == -1) { 4670 return EMPTY; 4671 } 4672 return str2.substring(at); 4673 } 4674 4675 4698 public static int indexOfDifference(String str1, String str2) { 4699 if (str1 == str2) { 4700 return -1; 4701 } 4702 if (str1 == null || str2 == null) { 4703 return 0; 4704 } 4705 int i; 4706 for (i = 0; i < str1.length() && i < str2.length(); ++i) { 4707 if (str1.charAt(i) != str2.charAt(i)) { 4708 break; 4709 } 4710 } 4711 if (i < str2.length() || i < str1.length()) { 4712 return i; 4713 } 4714 return -1; 4715 } 4716 4717 4748 public static int getLevenshteinDistance(String s, String t) { 4749 if (s == null || t == null) { 4750 throw new IllegalArgumentException ("Strings must not be null"); 4751 } 4752 int d[][]; int n; int m; int i; int j; char s_i; char t_j; int cost; 4761 n = s.length(); 4763 m = t.length(); 4764 if (n == 0) { 4765 return m; 4766 } 4767 if (m == 0) { 4768 return n; 4769 } 4770 d = new int[n + 1][m + 1]; 4771 4772 for (i = 0; i <= n; i++) { 4774 d[i][0] = i; 4775 } 4776 4777 for (j = 0; j <= m; j++) { 4778 d[0][j] = j; 4779 } 4780 4781 for (i = 1; i <= n; i++) { 4783 s_i = s.charAt(i - 1); 4784 4785 for (j = 1; j <= m; j++) { 4787 t_j = t.charAt(j - 1); 4788 4789 if (s_i == t_j) { 4791 cost = 0; 4792 } else { 4793 cost = 1; 4794 } 4795 4796 d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); 4798 } 4799 } 4800 4801 return d[n][m]; 4803 } 4804 4805 4813 private static int min(int a, int b, int c) { 4814 if (b < a) { 4816 a = b; 4817 } 4818 if (c < a) { 4819 a = c; 4820 } 4821 return a; 4822 } 4823 4824} 4825 | Popular Tags |