1 18 19 package com.Ostermiller.util; 20 21 import java.util.HashMap ; 22 import java.util.regex.Pattern ; 23 24 32 public class StringHelper { 33 34 49 public static String prepad(String s, int length){ 50 return prepad(s, length, ' '); 51 } 52 53 69 public static String prepad(String s, int length, char c){ 70 int needed = length - s.length(); 71 if (needed <= 0){ 72 return s; 73 } 74 StringBuffer sb = new StringBuffer (length); 75 for (int i=0; i<needed; i++){ 76 sb.append(c); 77 } 78 sb.append(s); 79 return (sb.toString()); 80 } 81 82 97 public static String postpad(String s, int length){ 98 return postpad(s, length, ' '); 99 } 100 101 117 public static String postpad(String s, int length, char c){ 118 int needed = length - s.length(); 119 if (needed <= 0){ 120 return s; 121 } 122 StringBuffer sb = new StringBuffer (length); 123 sb.append(s); 124 for (int i=0; i<needed; i++){ 125 sb.append(c); 126 } 127 return (sb.toString()); 128 } 129 130 150 public static String midpad(String s, int length){ 151 return midpad(s, length, ' '); 152 } 153 154 175 public static String midpad(String s, int length, char c){ 176 int needed = length - s.length(); 177 if (needed <= 0){ 178 return s; 179 } 180 int beginning = needed / 2; 181 int end = beginning + needed % 2; 182 StringBuffer sb = new StringBuffer (length); 183 for (int i=0; i<beginning; i++){ 184 sb.append(c); 185 } 186 sb.append(s); 187 for (int i=0; i<end; i++){ 188 sb.append(c); 189 } 190 return (sb.toString()); 191 } 192 193 227 public static String [] split(String s, String delimiter){ 228 int delimiterLength; 229 int stringLength = s.length(); 232 if (delimiter == null || (delimiterLength = delimiter.length()) == 0){ 233 return new String [] {s}; 241 } 242 243 248 int count; 249 int start; 250 int end; 251 252 count = 0; 254 start = 0; 255 while((end = s.indexOf(delimiter, start)) != -1) { 256 count++; 257 start = end + delimiterLength; 258 } 259 count++; 260 261 String [] result = new String [count]; 264 265 count = 0; 267 start = 0; 268 while((end = s.indexOf(delimiter, start)) != -1) { 269 result[count] = (s.substring(start, end)); 270 count++; 271 start = end + delimiterLength; 272 } 273 end = stringLength; 274 result[count] = s.substring(start, end); 275 276 return (result); 277 } 278 279 301 public static String replace(String s, String find, String replace){ 302 int findLength; 303 int stringLength = s.length(); 306 if (find == null || (findLength = find.length()) == 0){ 307 return s; 309 } 310 if (replace == null){ 311 replace = ""; 314 } 315 int replaceLength = replace.length(); 316 317 int length; 323 if (findLength == replaceLength){ 324 length = stringLength; 327 } else { 328 int count; 329 int start; 330 int end; 331 332 count = 0; 334 start = 0; 335 while((end = s.indexOf(find, start)) != -1) { 336 count++; 337 start = end + findLength; 338 } 339 if (count == 0){ 340 return s; 343 } 344 length = stringLength - (count * (findLength - replaceLength)); 345 } 346 347 int start = 0; 348 int end = s.indexOf(find, start); 349 if (end == -1){ 350 return s; 355 } 356 StringBuffer sb = new StringBuffer (length); 359 360 while (end != -1) { 362 sb.append(s.substring(start, end)); 363 sb.append(replace); 364 start = end + findLength; 365 end = s.indexOf(find, start); 366 } 367 end = stringLength; 368 sb.append(s.substring(start, end)); 369 370 return (sb.toString()); 371 } 372 373 411 public static String escapeHTML(String s){ 412 int length = s.length(); 413 int newLength = length; 414 boolean someCharacterEscaped = false; 415 for (int i=0; i<length; i++){ 419 char c = s.charAt(i); 420 int cint = 0xffff & c; 421 if (cint < 32){ 422 switch(c){ 423 case '\r': 424 case '\n': 425 case '\t': 426 case '\f':{ 427 } break; 428 default: { 429 newLength -= 1; 430 someCharacterEscaped = true; 431 } 432 } 433 } else { 434 switch(c){ 435 case '\"':{ 436 newLength += 5; 437 someCharacterEscaped = true; 438 } break; 439 case '&': 440 case '\'':{ 441 newLength += 4; 442 someCharacterEscaped = true; 443 } break; 444 case '<': 445 case '>':{ 446 newLength += 3; 447 someCharacterEscaped = true; 448 } break; 449 } 450 } 451 } 452 if (!someCharacterEscaped){ 453 return s; 455 } 456 StringBuffer sb = new StringBuffer (newLength); 457 for (int i=0; i<length; i++){ 458 char c = s.charAt(i); 459 int cint = 0xffff & c; 460 if (cint < 32){ 461 switch(c){ 462 case '\r': 463 case '\n': 464 case '\t': 465 case '\f':{ 466 sb.append(c); 467 } break; 468 default: { 469 } 471 } 472 } else { 473 switch(c){ 474 case '\"':{ 475 sb.append("""); 476 } break; 477 case '\'':{ 478 sb.append("'"); 479 } break; 480 case '&':{ 481 sb.append("&"); 482 } break; 483 case '<':{ 484 sb.append("<"); 485 } break; 486 case '>':{ 487 sb.append(">"); 488 } break; 489 default: { 490 sb.append(c); 491 } 492 } 493 } 494 } 495 return sb.toString(); 496 } 497 498 524 public static String escapeSQL(String s){ 525 int length = s.length(); 526 int newLength = length; 527 for (int i=0; i<length; i++){ 531 char c = s.charAt(i); 532 switch(c){ 533 case '\\': 534 case '\"': 535 case '\'': 536 case '\0':{ 537 newLength += 1; 538 } break; 539 } 540 } 541 if (length == newLength){ 542 return s; 544 } 545 StringBuffer sb = new StringBuffer (newLength); 546 for (int i=0; i<length; i++){ 547 char c = s.charAt(i); 548 switch(c){ 549 case '\\':{ 550 sb.append("\\\\"); 551 } break; 552 case '\"':{ 553 sb.append("\\\""); 554 } break; 555 case '\'':{ 556 sb.append("\\\'"); 557 } break; 558 case '\0':{ 559 sb.append("\\0"); 560 } break; 561 default: { 562 sb.append(c); 563 } 564 } 565 } 566 return sb.toString(); 567 } 568 569 581 public static String escapeJavaLiteral(String s){ 582 int length = s.length(); 583 int newLength = length; 584 for (int i=0; i<length; i++){ 588 char c = s.charAt(i); 589 switch(c){ 590 case '\"': 591 case '\'': 592 case '\n': 593 case '\r': 594 case '\t': 595 case '\\':{ 596 newLength += 1; 597 } break; 598 } 599 } 600 if (length == newLength){ 601 return s; 603 } 604 StringBuffer sb = new StringBuffer (newLength); 605 for (int i=0; i<length; i++){ 606 char c = s.charAt(i); 607 switch(c){ 608 case '\"':{ 609 sb.append("\\\""); 610 } break; 611 case '\'':{ 612 sb.append("\\\'"); 613 } break; 614 case '\n':{ 615 sb.append("\\n"); 616 } break; 617 case '\r':{ 618 sb.append("\\r"); 619 } break; 620 case '\t':{ 621 sb.append("\\t"); 622 } break; 623 case '\\':{ 624 sb.append("\\\\"); 625 } break; 626 default: { 627 sb.append(c); 628 } 629 } 630 } 631 return sb.toString(); 632 } 633 634 645 public static String trim(String s, String c){ 646 int length = s.length(); 647 if (c == null){ 648 return s; 649 } 650 int cLength = c.length(); 651 if (c.length() == 0){ 652 return s; 653 } 654 int start = 0; 655 int end = length; 656 boolean found; int i; 658 found = false; 661 for (i=0; !found && i<length; i++){ 662 char ch = s.charAt(i); 663 found = true; 664 for (int j=0; found && j<cLength; j++){ 665 if (c.charAt(j) == ch) found = false; 666 } 667 } 668 if (!found) return ""; 670 start = i-1; 671 found = false; 674 for (i=length-1; !found && i>=0; i--){ 675 char ch = s.charAt(i); 676 found = true; 677 for (int j=0; found && j<cLength; j++){ 678 if (c.charAt(j) == ch) found = false; 679 } 680 } 681 end = i+2; 682 return s.substring(start, end); 683 } 684 685 private static HashMap <String , Integer > htmlEntities = new HashMap <String , Integer >(); 686 static { 687 htmlEntities.put("nbsp", new Integer (160)); 688 htmlEntities.put("iexcl", new Integer (161)); 689 htmlEntities.put("cent", new Integer (162)); 690 htmlEntities.put("pound", new Integer (163)); 691 htmlEntities.put("curren", new Integer (164)); 692 htmlEntities.put("yen", new Integer (165)); 693 htmlEntities.put("brvbar", new Integer (166)); 694 htmlEntities.put("sect", new Integer (167)); 695 htmlEntities.put("uml", new Integer (168)); 696 htmlEntities.put("copy", new Integer (169)); 697 htmlEntities.put("ordf", new Integer (170)); 698 htmlEntities.put("laquo", new Integer (171)); 699 htmlEntities.put("not", new Integer (172)); 700 htmlEntities.put("shy", new Integer (173)); 701 htmlEntities.put("reg", new Integer (174)); 702 htmlEntities.put("macr", new Integer (175)); 703 htmlEntities.put("deg", new Integer (176)); 704 htmlEntities.put("plusmn", new Integer (177)); 705 htmlEntities.put("sup2", new Integer (178)); 706 htmlEntities.put("sup3", new Integer (179)); 707 htmlEntities.put("acute", new Integer (180)); 708 htmlEntities.put("micro", new Integer (181)); 709 htmlEntities.put("para", new Integer (182)); 710 htmlEntities.put("middot", new Integer (183)); 711 htmlEntities.put("cedil", new Integer (184)); 712 htmlEntities.put("sup1", new Integer (185)); 713 htmlEntities.put("ordm", new Integer (186)); 714 htmlEntities.put("raquo", new Integer (187)); 715 htmlEntities.put("frac14", new Integer (188)); 716 htmlEntities.put("frac12", new Integer (189)); 717 htmlEntities.put("frac34", new Integer (190)); 718 htmlEntities.put("iquest", new Integer (191)); 719 htmlEntities.put("Agrave", new Integer (192)); 720 htmlEntities.put("Aacute", new Integer (193)); 721 htmlEntities.put("Acirc", new Integer (194)); 722 htmlEntities.put("Atilde", new Integer (195)); 723 htmlEntities.put("Auml", new Integer (196)); 724 htmlEntities.put("Aring", new Integer (197)); 725 htmlEntities.put("AElig", new Integer (198)); 726 htmlEntities.put("Ccedil", new Integer (199)); 727 htmlEntities.put("Egrave", new Integer (200)); 728 htmlEntities.put("Eacute", new Integer (201)); 729 htmlEntities.put("Ecirc", new Integer (202)); 730 htmlEntities.put("Euml", new Integer (203)); 731 htmlEntities.put("Igrave", new Integer (204)); 732 htmlEntities.put("Iacute", new Integer (205)); 733 htmlEntities.put("Icirc", new Integer (206)); 734 htmlEntities.put("Iuml", new Integer (207)); 735 htmlEntities.put("ETH", new Integer (208)); 736 htmlEntities.put("Ntilde", new Integer (209)); 737 htmlEntities.put("Ograve", new Integer (210)); 738 htmlEntities.put("Oacute", new Integer (211)); 739 htmlEntities.put("Ocirc", new Integer (212)); 740 htmlEntities.put("Otilde", new Integer (213)); 741 htmlEntities.put("Ouml", new Integer (214)); 742 htmlEntities.put("times", new Integer (215)); 743 htmlEntities.put("Oslash", new Integer (216)); 744 htmlEntities.put("Ugrave", new Integer (217)); 745 htmlEntities.put("Uacute", new Integer (218)); 746 htmlEntities.put("Ucirc", new Integer (219)); 747 htmlEntities.put("Uuml", new Integer (220)); 748 htmlEntities.put("Yacute", new Integer (221)); 749 htmlEntities.put("THORN", new Integer (222)); 750 htmlEntities.put("szlig", new Integer (223)); 751 htmlEntities.put("agrave", new Integer (224)); 752 htmlEntities.put("aacute", new Integer (225)); 753 htmlEntities.put("acirc", new Integer (226)); 754 htmlEntities.put("atilde", new Integer (227)); 755 htmlEntities.put("auml", new Integer (228)); 756 htmlEntities.put("aring", new Integer (229)); 757 htmlEntities.put("aelig", new Integer (230)); 758 htmlEntities.put("ccedil", new Integer (231)); 759 htmlEntities.put("egrave", new Integer (232)); 760 htmlEntities.put("eacute", new Integer (233)); 761 htmlEntities.put("ecirc", new Integer (234)); 762 htmlEntities.put("euml", new Integer (235)); 763 htmlEntities.put("igrave", new Integer (236)); 764 htmlEntities.put("iacute", new Integer (237)); 765 htmlEntities.put("icirc", new Integer (238)); 766 htmlEntities.put("iuml", new Integer (239)); 767 htmlEntities.put("eth", new Integer (240)); 768 htmlEntities.put("ntilde", new Integer (241)); 769 htmlEntities.put("ograve", new Integer (242)); 770 htmlEntities.put("oacute", new Integer (243)); 771 htmlEntities.put("ocirc", new Integer (244)); 772 htmlEntities.put("otilde", new Integer (245)); 773 htmlEntities.put("ouml", new Integer (246)); 774 htmlEntities.put("divide", new Integer (247)); 775 htmlEntities.put("oslash", new Integer (248)); 776 htmlEntities.put("ugrave", new Integer (249)); 777 htmlEntities.put("uacute", new Integer (250)); 778 htmlEntities.put("ucirc", new Integer (251)); 779 htmlEntities.put("uuml", new Integer (252)); 780 htmlEntities.put("yacute", new Integer (253)); 781 htmlEntities.put("thorn", new Integer (254)); 782 htmlEntities.put("yuml", new Integer (255)); 783 htmlEntities.put("fnof", new Integer (402)); 784 htmlEntities.put("Alpha", new Integer (913)); 785 htmlEntities.put("Beta", new Integer (914)); 786 htmlEntities.put("Gamma", new Integer (915)); 787 htmlEntities.put("Delta", new Integer (916)); 788 htmlEntities.put("Epsilon", new Integer (917)); 789 htmlEntities.put("Zeta", new Integer (918)); 790 htmlEntities.put("Eta", new Integer (919)); 791 htmlEntities.put("Theta", new Integer (920)); 792 htmlEntities.put("Iota", new Integer (921)); 793 htmlEntities.put("Kappa", new Integer (922)); 794 htmlEntities.put("Lambda", new Integer (923)); 795 htmlEntities.put("Mu", new Integer (924)); 796 htmlEntities.put("Nu", new Integer (925)); 797 htmlEntities.put("Xi", new Integer (926)); 798 htmlEntities.put("Omicron", new Integer (927)); 799 htmlEntities.put("Pi", new Integer (928)); 800 htmlEntities.put("Rho", new Integer (929)); 801 htmlEntities.put("Sigma", new Integer (931)); 802 htmlEntities.put("Tau", new Integer (932)); 803 htmlEntities.put("Upsilon", new Integer (933)); 804 htmlEntities.put("Phi", new Integer (934)); 805 htmlEntities.put("Chi", new Integer (935)); 806 htmlEntities.put("Psi", new Integer (936)); 807 htmlEntities.put("Omega", new Integer (937)); 808 htmlEntities.put("alpha", new Integer (945)); 809 htmlEntities.put("beta", new Integer (946)); 810 htmlEntities.put("gamma", new Integer (947)); 811 htmlEntities.put("delta", new Integer (948)); 812 htmlEntities.put("epsilon", new Integer (949)); 813 htmlEntities.put("zeta", new Integer (950)); 814 htmlEntities.put("eta", new Integer (951)); 815 htmlEntities.put("theta", new Integer (952)); 816 htmlEntities.put("iota", new Integer (953)); 817 htmlEntities.put("kappa", new Integer (954)); 818 htmlEntities.put("lambda", new Integer (955)); 819 htmlEntities.put("mu", new Integer (956)); 820 htmlEntities.put("nu", new Integer (957)); 821 htmlEntities.put("xi", new Integer (958)); 822 htmlEntities.put("omicron", new Integer (959)); 823 htmlEntities.put("pi", new Integer (960)); 824 htmlEntities.put("rho", new Integer (961)); 825 htmlEntities.put("sigmaf", new Integer (962)); 826 htmlEntities.put("sigma", new Integer (963)); 827 htmlEntities.put("tau", new Integer (964)); 828 htmlEntities.put("upsilon", new Integer (965)); 829 htmlEntities.put("phi", new Integer (966)); 830 htmlEntities.put("chi", new Integer (967)); 831 htmlEntities.put("psi", new Integer (968)); 832 htmlEntities.put("omega", new Integer (969)); 833 htmlEntities.put("thetasym", new Integer (977)); 834 htmlEntities.put("upsih", new Integer (978)); 835 htmlEntities.put("piv", new Integer (982)); 836 htmlEntities.put("bull", new Integer (8226)); 837 htmlEntities.put("hellip", new Integer (8230)); 838 htmlEntities.put("prime", new Integer (8242)); 839 htmlEntities.put("Prime", new Integer (8243)); 840 htmlEntities.put("oline", new Integer (8254)); 841 htmlEntities.put("frasl", new Integer (8260)); 842 htmlEntities.put("weierp", new Integer (8472)); 843 htmlEntities.put("image", new Integer (8465)); 844 htmlEntities.put("real", new Integer (8476)); 845 htmlEntities.put("trade", new Integer (8482)); 846 htmlEntities.put("alefsym", new Integer (8501)); 847 htmlEntities.put("larr", new Integer (8592)); 848 htmlEntities.put("uarr", new Integer (8593)); 849 htmlEntities.put("rarr", new Integer (8594)); 850 htmlEntities.put("darr", new Integer (8595)); 851 htmlEntities.put("harr", new Integer (8596)); 852 htmlEntities.put("crarr", new Integer (8629)); 853 htmlEntities.put("lArr", new Integer (8656)); 854 htmlEntities.put("uArr", new Integer (8657)); 855 htmlEntities.put("rArr", new Integer (8658)); 856 htmlEntities.put("dArr", new Integer (8659)); 857 htmlEntities.put("hArr", new Integer (8660)); 858 htmlEntities.put("forall", new Integer (8704)); 859 htmlEntities.put("part", new Integer (8706)); 860 htmlEntities.put("exist", new Integer (8707)); 861 htmlEntities.put("empty", new Integer (8709)); 862 htmlEntities.put("nabla", new Integer (8711)); 863 htmlEntities.put("isin", new Integer (8712)); 864 htmlEntities.put("notin", new Integer (8713)); 865 htmlEntities.put("ni", new Integer (8715)); 866 htmlEntities.put("prod", new Integer (8719)); 867 htmlEntities.put("sum", new Integer (8721)); 868 htmlEntities.put("minus", new Integer (8722)); 869 htmlEntities.put("lowast", new Integer (8727)); 870 htmlEntities.put("radic", new Integer (8730)); 871 htmlEntities.put("prop", new Integer (8733)); 872 htmlEntities.put("infin", new Integer (8734)); 873 htmlEntities.put("ang", new Integer (8736)); 874 htmlEntities.put("and", new Integer (8743)); 875 htmlEntities.put("or", new Integer (8744)); 876 htmlEntities.put("cap", new Integer (8745)); 877 htmlEntities.put("cup", new Integer (8746)); 878 htmlEntities.put("int", new Integer (8747)); 879 htmlEntities.put("there4", new Integer (8756)); 880 htmlEntities.put("sim", new Integer (8764)); 881 htmlEntities.put("cong", new Integer (8773)); 882 htmlEntities.put("asymp", new Integer (8776)); 883 htmlEntities.put("ne", new Integer (8800)); 884 htmlEntities.put("equiv", new Integer (8801)); 885 htmlEntities.put("le", new Integer (8804)); 886 htmlEntities.put("ge", new Integer (8805)); 887 htmlEntities.put("sub", new Integer (8834)); 888 htmlEntities.put("sup", new Integer (8835)); 889 htmlEntities.put("nsub", new Integer (8836)); 890 htmlEntities.put("sube", new Integer (8838)); 891 htmlEntities.put("supe", new Integer (8839)); 892 htmlEntities.put("oplus", new Integer (8853)); 893 htmlEntities.put("otimes", new Integer (8855)); 894 htmlEntities.put("perp", new Integer (8869)); 895 htmlEntities.put("sdot", new Integer (8901)); 896 htmlEntities.put("lceil", new Integer (8968)); 897 htmlEntities.put("rceil", new Integer (8969)); 898 htmlEntities.put("lfloor", new Integer (8970)); 899 htmlEntities.put("rfloor", new Integer (8971)); 900 htmlEntities.put("lang", new Integer (9001)); 901 htmlEntities.put("rang", new Integer (9002)); 902 htmlEntities.put("loz", new Integer (9674)); 903 htmlEntities.put("spades", new Integer (9824)); 904 htmlEntities.put("clubs", new Integer (9827)); 905 htmlEntities.put("hearts", new Integer (9829)); 906 htmlEntities.put("diams", new Integer (9830)); 907 htmlEntities.put("quot", new Integer (34)); 908 htmlEntities.put("amp", new Integer (38)); 909 htmlEntities.put("lt", new Integer (60)); 910 htmlEntities.put("gt", new Integer (62)); 911 htmlEntities.put("OElig", new Integer (338)); 912 htmlEntities.put("oelig", new Integer (339)); 913 htmlEntities.put("Scaron", new Integer (352)); 914 htmlEntities.put("scaron", new Integer (353)); 915 htmlEntities.put("Yuml", new Integer (376)); 916 htmlEntities.put("circ", new Integer (710)); 917 htmlEntities.put("tilde", new Integer (732)); 918 htmlEntities.put("ensp", new Integer (8194)); 919 htmlEntities.put("emsp", new Integer (8195)); 920 htmlEntities.put("thinsp", new Integer (8201)); 921 htmlEntities.put("zwnj", new Integer (8204)); 922 htmlEntities.put("zwj", new Integer (8205)); 923 htmlEntities.put("lrm", new Integer (8206)); 924 htmlEntities.put("rlm", new Integer (8207)); 925 htmlEntities.put("ndash", new Integer (8211)); 926 htmlEntities.put("mdash", new Integer (8212)); 927 htmlEntities.put("lsquo", new Integer (8216)); 928 htmlEntities.put("rsquo", new Integer (8217)); 929 htmlEntities.put("sbquo", new Integer (8218)); 930 htmlEntities.put("ldquo", new Integer (8220)); 931 htmlEntities.put("rdquo", new Integer (8221)); 932 htmlEntities.put("bdquo", new Integer (8222)); 933 htmlEntities.put("dagger", new Integer (8224)); 934 htmlEntities.put("Dagger", new Integer (8225)); 935 htmlEntities.put("permil", new Integer (8240)); 936 htmlEntities.put("lsaquo", new Integer (8249)); 937 htmlEntities.put("rsaquo", new Integer (8250)); 938 htmlEntities.put("euro", new Integer (8364)); 939 } 940 941 951 public static String unescapeHTML(String s){ 952 StringBuffer result = new StringBuffer (s.length()); 953 int ampInd = s.indexOf("&"); 954 int lastEnd = 0; 955 while (ampInd >= 0){ 956 int nextAmp = s.indexOf("&", ampInd+1); 957 int nextSemi = s.indexOf(";", ampInd+1); 958 if (nextSemi != -1 && (nextAmp == -1 || nextSemi < nextAmp)){ 959 int value = -1; 960 String escape = s.substring(ampInd+1,nextSemi); 961 try { 962 if (escape.startsWith("#")){ 963 value = Integer.parseInt(escape.substring(1), 10); 964 } else { 965 if (htmlEntities.containsKey(escape)){ 966 value = ((Integer )(htmlEntities.get(escape))).intValue(); 967 } 968 } 969 } catch (NumberFormatException x){ 970 } 971 result.append(s.substring(lastEnd, ampInd)); 972 lastEnd = nextSemi + 1; 973 if (value >= 0 && value <= 0xffff){ 974 result.append((char)value); 975 } else { 976 result.append("&").append(escape).append(";"); 977 } 978 } 979 ampInd = nextAmp; 980 } 981 result.append(s.substring(lastEnd)); 982 return result.toString(); 983 } 984 985 995 public static String escapeRegularExpressionLiteral(String s){ 996 1011 int length = s.length(); 1012 int newLength = length; 1013 for (int i=0; i<length; i++){ 1017 char c = s.charAt(i); 1018 if (!((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'))){ 1019 newLength += 1; 1020 } 1021 } 1022 if (length == newLength){ 1023 return s; 1025 } 1026 StringBuffer sb = new StringBuffer (newLength); 1027 for (int i=0; i<length; i++){ 1028 char c = s.charAt(i); 1029 if (!((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'))){ 1030 sb.append('\\'); 1031 } 1032 sb.append(c); 1033 } 1034 return sb.toString(); 1035 } 1036 1037 1046 private static void buildFindAnyPattern(String [] terms, StringBuffer sb){ 1047 if (terms.length == 0) throw new IllegalArgumentException ("There must be at least one term to find."); 1048 sb.append("(?:"); 1049 for (int i=0; i<terms.length; i++){ 1050 if (i>0) sb.append("|"); 1051 sb.append("(?:"); 1052 sb.append(escapeRegularExpressionLiteral(terms[i])); 1053 sb.append(")"); 1054 } 1055 sb.append(")"); 1056 } 1057 1058 1073 public static Pattern getContainsAnyPattern(String [] terms){ 1074 StringBuffer sb = new StringBuffer (); 1075 sb.append("(?s).*"); 1076 buildFindAnyPattern(terms, sb); 1077 sb.append(".*"); 1078 return Pattern.compile(sb.toString()); 1079 } 1080 1081 1096 public static Pattern getEqualsAnyPattern(String [] terms){ 1097 StringBuffer sb = new StringBuffer (); 1098 sb.append("(?s)\\A"); 1099 buildFindAnyPattern(terms, sb); 1100 sb.append("\\z"); 1101 return Pattern.compile(sb.toString()); 1102 } 1103 1104 1119 public static Pattern getStartsWithAnyPattern(String [] terms){ 1120 StringBuffer sb = new StringBuffer (); 1121 sb.append("(?s)\\A"); 1122 buildFindAnyPattern(terms, sb); 1123 sb.append(".*"); 1124 return Pattern.compile(sb.toString()); 1125 } 1126 1127 1142 public static Pattern getEndsWithAnyPattern(String [] terms){ 1143 StringBuffer sb = new StringBuffer (); 1144 sb.append("(?s).*"); 1145 buildFindAnyPattern(terms, sb); 1146 sb.append("\\z"); 1147 return Pattern.compile(sb.toString()); 1148 } 1149 1150 1167 public static Pattern getContainsAnyIgnoreCasePattern(String [] terms){ 1168 StringBuffer sb = new StringBuffer (); 1169 sb.append("(?i)(?u)(?s).*"); 1170 buildFindAnyPattern(terms, sb); 1171 sb.append(".*"); 1172 return Pattern.compile(sb.toString()); 1173 } 1174 1175 1192 public static Pattern getEqualsAnyIgnoreCasePattern(String [] terms){ 1193 StringBuffer sb = new StringBuffer (); 1194 sb.append("(?i)(?u)(?s)\\A"); 1195 buildFindAnyPattern(terms, sb); 1196 sb.append("\\z"); 1197 return Pattern.compile(sb.toString()); 1198 } 1199 1200 1217 public static Pattern getStartsWithAnyIgnoreCasePattern(String [] terms){ 1218 StringBuffer sb = new StringBuffer (); 1219 sb.append("(?i)(?u)(?s)\\A"); 1220 buildFindAnyPattern(terms, sb); 1221 sb.append(".*"); 1222 return Pattern.compile(sb.toString()); 1223 } 1224 1225 1242 public static Pattern getEndsWithAnyIgnoreCasePattern(String [] terms){ 1243 StringBuffer sb = new StringBuffer (); 1244 sb.append("(?i)(?u)(?s).*"); 1245 buildFindAnyPattern(terms, sb); 1246 sb.append("\\z"); 1247 return Pattern.compile(sb.toString()); 1248 } 1249 1250 1269 public static boolean containsAny(String s, String [] terms){ 1270 return getContainsAnyPattern(terms).matcher(s).matches(); 1271 } 1272 1273 1292 public static boolean equalsAny(String s, String [] terms){ 1293 return getEqualsAnyPattern(terms).matcher(s).matches(); 1294 } 1295 1296 1315 public static boolean startsWithAny(String s, String [] terms){ 1316 return getStartsWithAnyPattern(terms).matcher(s).matches(); 1317 } 1318 1319 1338 public static boolean endsWithAny(String s, String [] terms){ 1339 return getEndsWithAnyPattern(terms).matcher(s).matches(); 1340 } 1341 1342 1363 public static boolean containsAnyIgnoreCase(String s, String [] terms){ 1364 return getContainsAnyIgnoreCasePattern(terms).matcher(s).matches(); 1365 } 1366 1367 1388 public static boolean equalsAnyIgnoreCase(String s, String [] terms){ 1389 return getEqualsAnyIgnoreCasePattern(terms).matcher(s).matches(); 1390 } 1391 1392 1413 public static boolean startsWithAnyIgnoreCase(String s, String [] terms){ 1414 return getStartsWithAnyIgnoreCasePattern(terms).matcher(s).matches(); 1415 } 1416 1417 1438 public static boolean endsWithAnyIgnoreCase(String s, String [] terms){ 1439 return getEndsWithAnyIgnoreCasePattern(terms).matcher(s).matches(); 1440 } 1441} 1442 | Popular Tags |