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 return count(source, c, 0); 859 } 860 public static int count(String source, char c, int start) { 861 int count = 0; 862 int j = start; 863 while (true) { 864 int i = source.indexOf(c, j); 865 if (i == -1) { 866 break; 867 } 868 count++; 869 j = i + 1; 870 } 871 return count; 872 } 873 874 875 876 883 public static int countIgnoreCase(String source, String sub) { 884 int count = 0; 885 int j = 0; 886 while (true) { 887 int i = indexOfIgnoreCase(source, sub, j); 888 if (i == -1) { 889 break; 890 } 891 count++; 892 j = i + sub.length(); 893 } 894 return count; 895 } 896 897 899 910 public static int[] indexOf(String s, String arr[]) { 911 return indexOf(s, arr, 0); 912 } 913 925 public static int[] indexOf(String s, String arr[], int start) { 926 int arrLen = arr.length; 927 int index = Integer.MAX_VALUE; 928 int last = -1; 929 for (int j = 0; j < arrLen; j++) { 930 int i = s.indexOf(arr[j], start); 931 if (i != -1) { 932 if (i < index) { 933 index = i; 934 last = j; 935 } 936 } 937 } 938 return new int[] {last, index}; 939 } 940 941 952 public static int[] indexOfIgnoreCase(String s, String arr[]) { 953 return indexOfIgnoreCase(s, arr, 0); 954 } 955 967 public static int[] indexOfIgnoreCase(String s, String arr[], int start) { 968 int arrLen = arr.length; 969 int index = Integer.MAX_VALUE; 970 int last = -1; 971 for (int j = 0; j < arrLen; j++) { 972 int i = indexOfIgnoreCase(s, arr[j], start); 973 if (i != -1) { 974 if (i < index) { 975 index = i; 976 last = j; 977 } 978 } 979 } 980 return new int[] {last, index}; 981 } 982 983 994 public static int[] lastIndexOf(String s, String arr[]) { 995 return lastIndexOf(s, arr, s.length()); 996 } 997 1009 public static int[] lastIndexOf(String s, String arr[], int fromIndex) { 1010 int arrLen = arr.length; 1011 int index = -1; 1012 int last = -1; 1013 for (int j = 0; j < arrLen; j++) { 1014 int i = s.lastIndexOf(arr[j], fromIndex); 1015 if (i != -1) { 1016 if (i > index) { 1017 index = i; 1018 last = j; 1019 } 1020 } 1021 } 1022 return new int[] {last, index}; 1023 } 1024 1025 1036 public static int[] lastIndexOfIgnoreCase(String s, String arr[]) { 1037 return lastIndexOfIgnoreCase(s, arr, s.length()); 1038 } 1039 1051 public static int[] lastIndexOfIgnoreCase(String s, String arr[], int fromIndex) { 1052 int arrLen = arr.length; 1053 int index = -1; 1054 int last = -1; 1055 for (int j = 0; j < arrLen; j++) { 1056 int i = lastIndexOfIgnoreCase(s, arr[j], fromIndex); 1057 if (i != -1) { 1058 if (i > index) { 1059 index = i; 1060 last = j; 1061 } 1062 } 1063 } 1064 return new int[] {last, index}; 1065 } 1066 1067 1075 public static boolean equals(String as[], String as1[]) { 1076 if (as.length != as1.length) { 1077 return false; 1078 } 1079 for (int i = 0; i < as.length; i++) { 1080 if (as[i].equals(as1[i]) == false) { 1081 return false; 1082 } 1083 } 1084 return true; 1085 } 1086 1094 public static boolean equalsIgnoreCase(String as[], String as1[]) { 1095 if (as.length != as1.length) { 1096 return false; 1097 } 1098 for (int i = 0; i < as.length; i++) { 1099 if (as[i].equalsIgnoreCase(as1[i]) == false) { 1100 return false; 1101 } 1102 } 1103 return true; 1104 } 1105 1106 1107 1116 public static String replace(String s, String [] sub, String [] with) { 1117 if ((sub.length != with.length) || (sub.length == 0)) { 1118 return s; 1119 } 1120 int start = 0; 1121 StringBuffer buf = new StringBuffer (s.length()); 1122 while (true) { 1123 int[] res = indexOf(s, sub, start); 1124 if (res[0] == -1) { 1125 break; 1126 } 1127 int end = res[1]; 1128 buf.append(s.substring(start, end)); 1129 buf.append(with[res[0]]); 1130 start = end + sub[res[0]].length(); 1131 } 1132 buf.append(s.substring(start)); 1133 return buf.toString(); 1134 } 1135 1136 1145 public static String replaceIgnoreCase(String s, String [] sub, String [] with) { 1146 if ((sub.length != with.length) || (sub.length == 0)) { 1147 return s; 1148 } 1149 int start = 0; 1150 StringBuffer buf = new StringBuffer (s.length()); 1151 while (true) { 1152 int[] res = indexOfIgnoreCase(s, sub, start); 1153 if (res[0] == -1) { 1154 break; 1155 } 1156 int end = res[1]; 1157 buf.append(s.substring(start, end)); 1158 buf.append(with[res[0]]); 1159 start = end + sub[0].length(); 1160 } 1161 buf.append(s.substring(start)); 1162 return buf.toString(); 1163 } 1164 1165 1166 1168 1173 public static int equalsOne(String src, String [] dest) { 1174 for (int i = 0; i < dest.length; i++) { 1175 if (src.equals(dest[i])) { 1176 return i; 1177 } 1178 } 1179 return -1; 1180 } 1181 1186 public static int equalsOneIgnoreCase(String src, String [] dest) { 1187 for (int i = 0; i < dest.length; i++) { 1188 if (src.equalsIgnoreCase(dest[i])) { 1189 return i; 1190 } 1191 } 1192 return -1; 1193 } 1194 1195 1200 public static int startsWithOne(String src, String [] dest) { 1201 for (int i = 0; i < dest.length; i++) { 1202 if (src.startsWith(dest[i])) { 1203 return i; 1204 } 1205 } 1206 return -1; 1207 } 1208 1209 1214 public static int startsWithOneIgnoreCase(String src, String [] dest) { 1215 for (int i = 0; i < dest.length; i++) { 1216 if (startsWithIgnoreCase(src, dest[i])) { 1217 return i; 1218 } 1219 } 1220 return -1; 1221 } 1222 1223 1224 1229 public static int endsWithOne(String src, String [] dest) { 1230 for (int i = 0; i < dest.length; i++) { 1231 if (src.endsWith(dest[i])) { 1232 return i; 1233 } 1234 } 1235 return -1; 1236 } 1237 1238 1243 public static int endsWithOneIgnoreCase(String src, String [] dest) { 1244 for (int i = 0; i < dest.length; i++) { 1245 if (endsWithIgnoreCase(src, dest[i])) { 1246 return i; 1247 } 1248 } 1249 return -1; 1250 } 1251 1252 1253 1255 1256 public static int indexOfChars(String string, String chars) { 1257 return indexOfChars(string, chars, 0); 1258 } 1259 1260 1264 public static int indexOfChars(String string, String chars, int startindex) { 1265 int stringLen = string.length(); 1266 int charsLen = chars.length(); 1267 for (int i = startindex; i < stringLen; i++) { 1268 char c = string.charAt(i); 1269 for (int j = 0; j < charsLen; j++) { 1270 if (c == chars.charAt(j)) { 1271 return i; 1272 } 1273 } 1274 } 1275 return -1; 1276 } 1277 1278 public static int indexOfChars(String string, char[] chars) { 1279 return indexOfChars(string, chars, 0); 1280 } 1281 1282 1286 public static int indexOfChars(String string, char[] chars, int startindex) { 1287 int stringLen = string.length(); 1288 int charsLen = chars.length; 1289 for (int i = startindex; i < stringLen; i++) { 1290 char c = string.charAt(i); 1291 for (int j = 0; j < charsLen; j++) { 1292 if (c == chars[j]) { 1293 return i; 1294 } 1295 } 1296 } 1297 return -1; 1298 } 1299 1300 1301 1303 1304 1307 public static void trim(String [] strings) { 1308 for (int i = 0; i < strings.length; i++) { 1309 String string = strings[i]; 1310 if (string != null) { 1311 strings[i] = string.trim(); 1312 } 1313 1314 } 1315 } 1316 1317 1322 public static void trimNonEmpty(String [] strings) { 1323 for (int i = 0; i < strings.length; i++) { 1324 String string = strings[i]; 1325 if (string != null) { 1326 strings[i] = trimNonEmpty(strings[i]); 1327 } 1328 } 1329 } 1330 1331 1332 1335 public static String trimNonEmpty(String string) { 1336 if (string != null) { 1337 string = string.trim(); 1338 if (string.length() == 0) { 1339 string = null; 1340 } 1341 } 1342 return string; 1343 } 1344 1345 1346 1348 1351 public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary) { 1352 return indexOfRegion(string, leftBoundary, rightBoundary, 0); 1353 } 1354 1355 1356 1360 public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, int offset) { 1361 int ndx = offset; 1362 int[] res = new int[4]; 1363 ndx = string.indexOf(leftBoundary, ndx); 1364 if (ndx == -1) { 1365 return null; 1366 } 1367 res[0] = ndx; 1368 ndx += leftBoundary.length(); 1369 res[1] = ndx; 1370 1371 ndx = string.indexOf(rightBoundary, ndx); 1372 if (ndx == -1) { 1373 return null; 1374 } 1375 res[2] = ndx; 1376 res[3] = ndx + rightBoundary.length(); 1377 return res; 1378 } 1379 1380 1381 1384 public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, char escape) { 1385 return indexOfRegion(string, leftBoundary, rightBoundary, escape, 0); 1386 } 1387 1388 1401 public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, char escape, int offset) { 1402 int ndx = offset; 1403 int[] res = new int[4]; 1404 while (true) { 1405 ndx = string.indexOf(leftBoundary, ndx); 1406 if (ndx == -1) { 1407 return null; 1408 } 1409 if (ndx > 0) { 1410 if (string.charAt(ndx - 1) == escape) { 1411 ndx += leftBoundary.length(); 1412 continue; 1413 } 1414 } 1415 res[0] = ndx; 1416 ndx += leftBoundary.length(); 1417 res[1] = ndx; 1418 1419 while (true) { 1420 ndx = string.indexOf(rightBoundary, ndx); 1421 if (ndx == -1) { 1422 return null; 1423 } 1424 if (ndx > 0) { 1425 if (string.charAt(ndx - 1) == escape) { 1426 ndx += rightBoundary.length(); 1427 continue; 1428 } 1429 } 1430 res[2] = ndx; 1431 res[3] = ndx + rightBoundary.length(); 1432 return res; 1433 } 1434 } 1435 } 1436 1437 1438 1440 1443 public static String convertCharset(String source, String srcCharsetName, String newCharsetName) throws UnsupportedEncodingException { 1444 return new String (source.getBytes(srcCharsetName), newCharsetName); 1445 } 1446 1447} 1448 | Popular Tags |