1 16 package org.mortbay.util; 17 18 29 public class StringUtil 30 { 31 public static final String __LINE_SEPARATOR= 32 System.getProperty("line.separator","\n"); 33 34 public final static String __ISO_8859_1; 35 static 36 { 37 String iso=System.getProperty("ISO_8859_1"); 38 if (iso!=null) 39 __ISO_8859_1=iso; 40 else 41 { 42 try{ 43 new String (new byte[]{(byte)20},"ISO-8859-1"); 44 iso="ISO-8859-1"; 45 } 46 catch(java.io.UnsupportedEncodingException e) 47 { 48 iso="ISO8859_1"; 49 } 50 __ISO_8859_1=iso; 51 } 52 } 53 54 public final static String __UTF_8="UTF-8"; 55 56 private static char[] lowercases = { 57 '\000','\001','\002','\003','\004','\005','\006','\007', 58 '\010','\011','\012','\013','\014','\015','\016','\017', 59 '\020','\021','\022','\023','\024','\025','\026','\027', 60 '\030','\031','\032','\033','\034','\035','\036','\037', 61 '\040','\041','\042','\043','\044','\045','\046','\047', 62 '\050','\051','\052','\053','\054','\055','\056','\057', 63 '\060','\061','\062','\063','\064','\065','\066','\067', 64 '\070','\071','\072','\073','\074','\075','\076','\077', 65 '\100','\141','\142','\143','\144','\145','\146','\147', 66 '\150','\151','\152','\153','\154','\155','\156','\157', 67 '\160','\161','\162','\163','\164','\165','\166','\167', 68 '\170','\171','\172','\133','\134','\135','\136','\137', 69 '\140','\141','\142','\143','\144','\145','\146','\147', 70 '\150','\151','\152','\153','\154','\155','\156','\157', 71 '\160','\161','\162','\163','\164','\165','\166','\167', 72 '\170','\171','\172','\173','\174','\175','\176','\177' }; 73 74 75 80 public static String asciiToLowerCase(String s) 81 { 82 char[] c = null; 83 int i=s.length(); 84 85 while (i-->0) 87 { 88 char c1=s.charAt(i); 89 if (c1<=127) 90 { 91 char c2=lowercases[c1]; 92 if (c1!=c2) 93 { 94 c=s.toCharArray(); 95 c[i]=c2; 96 break; 97 } 98 } 99 } 100 101 while (i-->0) 102 { 103 if(c[i]<=127) 104 c[i] = lowercases[c[i]]; 105 } 106 107 return c==null?s:new String (c); 108 } 109 110 111 112 public static boolean startsWithIgnoreCase(String s,String w) 113 { 114 if (w==null) 115 return true; 116 117 if (s==null || s.length()<w.length()) 118 return false; 119 120 for (int i=0;i<w.length();i++) 121 { 122 char c1=s.charAt(i); 123 char c2=w.charAt(i); 124 if (c1!=c2) 125 { 126 if (c1<=127) 127 c1=lowercases[c1]; 128 if (c2<=127) 129 c2=lowercases[c2]; 130 if (c1!=c2) 131 return false; 132 } 133 } 134 return true; 135 } 136 137 138 public static boolean endsWithIgnoreCase(String s,String w) 139 { 140 if (w==null) 141 return true; 142 143 int sl=s.length(); 144 int wl=w.length(); 145 146 if (s==null || sl<wl) 147 return false; 148 149 for (int i=wl;i-->0;) 150 { 151 char c1=s.charAt(--sl); 152 char c2=w.charAt(i); 153 if (c1!=c2) 154 { 155 if (c1<=127) 156 c1=lowercases[c1]; 157 if (c2<=127) 158 c2=lowercases[c2]; 159 if (c1!=c2) 160 return false; 161 } 162 } 163 return true; 164 } 165 166 167 170 public static int indexFrom(String s,String chars) 171 { 172 for (int i=0;i<s.length();i++) 173 if (chars.indexOf(s.charAt(i))>=0) 174 return i; 175 return -1; 176 } 177 178 179 182 public static String replace(String s, String sub, String with) 183 { 184 int c=0; 185 int i=s.indexOf(sub,c); 186 if (i == -1) 187 return s; 188 189 StringBuffer buf = new StringBuffer (s.length()+with.length()); 190 191 synchronized(buf) 192 { 193 do 194 { 195 buf.append(s.substring(c,i)); 196 buf.append(with); 197 c=i+sub.length(); 198 } while ((i=s.indexOf(sub,c))!=-1); 199 200 if (c<s.length()) 201 buf.append(s.substring(c,s.length())); 202 203 return buf.toString(); 204 } 205 } 206 207 208 210 public static String unquote(String s) 211 { 212 if ((s.startsWith("\"") && s.endsWith("\"")) || 213 (s.startsWith("'") && s.endsWith("'"))) 214 s=s.substring(1,s.length()-1); 215 return s; 216 } 217 218 219 220 226 public static void append(StringBuffer buf, 227 String s, 228 int offset, 229 int length) 230 { 231 synchronized(buf) 232 { 233 int end=offset+length; 234 for (int i=offset; i<end;i++) 235 { 236 if (i>=s.length()) 237 break; 238 buf.append(s.charAt(i)); 239 } 240 } 241 } 242 243 244 245 public static void append(StringBuffer buf,byte b,int base) 246 { 247 int bi=0xff&b; 248 int c='0'+(bi/base)%base; 249 if (c>'9') 250 c= 'a'+(c-'0'-10); 251 buf.append((char)c); 252 c='0'+bi%base; 253 if (c>'9') 254 c= 'a'+(c-'0'-10); 255 buf.append((char)c); 256 } 257 258 259 public static void append2digits(StringBuffer buf,int i) 260 { 261 if (i<100) 262 { 263 buf.append((char)(i/10+'0')); 264 buf.append((char)(i%10+'0')); 265 } 266 } 267 268 269 273 public static String nonNull(String s) 274 { 275 if (s==null) 276 return ""; 277 return s; 278 } 279 280 281 public static boolean equals(String s,char[] buf, int offset, int length) 282 { 283 if (s.length()!=length) 284 return false; 285 for (int i=0;i<length;i++) 286 if (buf[offset+i]!=s.charAt(i)) 287 return false; 288 return true; 289 } 290 291 } 292 | Popular Tags |