1 16 package org.apache.commons.lang; 17 18 33 public class WordUtils { 34 35 43 public WordUtils() { 44 } 45 46 71 120 140 public static String wrap(String str, int wrapLength) { 141 return wrap(str, wrapLength, null, false); 142 } 143 144 162 public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) { 163 if (str == null) { 164 return null; 165 } 166 if (newLineStr == null) { 167 newLineStr = SystemUtils.LINE_SEPARATOR; 168 } 169 if (wrapLength < 1) { 170 wrapLength = 1; 171 } 172 int inputLineLength = str.length(); 173 int offset = 0; 174 StringBuffer wrappedLine = new StringBuffer (inputLineLength + 32); 175 176 while ((inputLineLength - offset) > wrapLength) { 177 if (str.charAt(offset) == ' ') { 178 offset++; 179 continue; 180 } 181 int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset); 182 183 if (spaceToWrapAt >= offset) { 184 wrappedLine.append(str.substring(offset, spaceToWrapAt)); 186 wrappedLine.append(newLineStr); 187 offset = spaceToWrapAt + 1; 188 189 } else { 190 if (wrapLongWords) { 192 wrappedLine.append(str.substring(offset, wrapLength + offset)); 194 wrappedLine.append(newLineStr); 195 offset += wrapLength; 196 } else { 197 spaceToWrapAt = str.indexOf(' ', wrapLength + offset); 199 if (spaceToWrapAt >= 0) { 200 wrappedLine.append(str.substring(offset, spaceToWrapAt)); 201 wrappedLine.append(newLineStr); 202 offset = spaceToWrapAt + 1; 203 } else { 204 wrappedLine.append(str.substring(offset)); 205 offset = inputLineLength; 206 } 207 } 208 } 209 } 210 211 wrappedLine.append(str.substring(offset)); 213 214 return wrappedLine.toString(); 215 } 216 217 241 public static String capitalize(String str) { 242 return capitalize(str, null); 243 } 244 245 274 public static String capitalize(String str, char[] delimiters) { 275 if (str == null || str.length() == 0) { 276 return str; 277 } 278 int strLen = str.length(); 279 StringBuffer buffer = new StringBuffer (strLen); 280 281 int delimitersLen = 0; 282 if(delimiters != null) { 283 delimitersLen = delimiters.length; 284 } 285 286 boolean capitalizeNext = true; 287 for (int i = 0; i < strLen; i++) { 288 char ch = str.charAt(i); 289 290 boolean isDelimiter = false; 291 if(delimiters == null) { 292 isDelimiter = Character.isWhitespace(ch); 293 } else { 294 for(int j=0; j < delimitersLen; j++) { 295 if(ch == delimiters[j]) { 296 isDelimiter = true; 297 break; 298 } 299 } 300 } 301 302 if (isDelimiter) { 303 buffer.append(ch); 304 capitalizeNext = true; 305 } else if (capitalizeNext) { 306 buffer.append(Character.toTitleCase(ch)); 307 capitalizeNext = false; 308 } else { 309 buffer.append(ch); 310 } 311 } 312 return buffer.toString(); 313 } 314 315 334 public static String capitalizeFully(String str) { 335 return capitalizeFully(str, null); 336 } 337 338 364 public static String capitalizeFully(String str, char[] delimiters) { 365 if (str == null || str.length() == 0) { 366 return str; 367 } 368 str = str.toLowerCase(); 369 return capitalize(str, delimiters); 370 } 371 372 389 public static String uncapitalize(String str) { 390 return uncapitalize(str, null); 391 } 392 393 418 public static String uncapitalize(String str, char[] delimiters) { 419 if (str == null || str.length() == 0) { 420 return str; 421 } 422 int strLen = str.length(); 423 424 int delimitersLen = 0; 425 if(delimiters != null) { 426 delimitersLen = delimiters.length; 427 } 428 429 StringBuffer buffer = new StringBuffer (strLen); 430 boolean uncapitalizeNext = true; 431 for (int i = 0; i < strLen; i++) { 432 char ch = str.charAt(i); 433 434 boolean isDelimiter = false; 435 if(delimiters == null) { 436 isDelimiter = Character.isWhitespace(ch); 437 } else { 438 for(int j=0; j < delimitersLen; j++) { 439 if(ch == delimiters[j]) { 440 isDelimiter = true; 441 break; 442 } 443 } 444 } 445 446 if (isDelimiter) { 447 buffer.append(ch); 448 uncapitalizeNext = true; 449 } else if (uncapitalizeNext) { 450 buffer.append(Character.toLowerCase(ch)); 451 uncapitalizeNext = false; 452 } else { 453 buffer.append(ch); 454 } 455 } 456 return buffer.toString(); 457 } 458 459 481 public static String swapCase(String str) { 482 int strLen; 483 if (str == null || (strLen = str.length()) == 0) { 484 return str; 485 } 486 StringBuffer buffer = new StringBuffer (strLen); 487 488 boolean whitespace = true; 489 char ch = 0; 490 char tmp = 0; 491 492 for (int i = 0; i < strLen; i++) { 493 ch = str.charAt(i); 494 if (Character.isUpperCase(ch)) { 495 tmp = Character.toLowerCase(ch); 496 } else if (Character.isTitleCase(ch)) { 497 tmp = Character.toLowerCase(ch); 498 } else if (Character.isLowerCase(ch)) { 499 if (whitespace) { 500 tmp = Character.toTitleCase(ch); 501 } else { 502 tmp = Character.toUpperCase(ch); 503 } 504 } else { 505 tmp = ch; 506 } 507 buffer.append(tmp); 508 whitespace = Character.isWhitespace(ch); 509 } 510 return buffer.toString(); 511 } 512 513 } 514 | Popular Tags |