RSS Feed for This PostCurrent Article

Apache Commons Lang Part I – String Functions

Download Sample Code

Apache Commons Lang provides very generic, very reusable components for everyday use.

String manipulation – StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils.

StringUtils operations are null safe. As taken from the documentation,

  • IsEmpty/IsBlank – checks if a String contains text
  • Trim/Strip – removes leading and trailing whitespace
  • Equals – compares two strings null-safe
  • IndexOf/LastIndexOf/Contains – null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut – index-of any of a set of Strings
  • ContainsOnly/ContainsNone – does String contains only/none of these characters
  • Substring/Left/Right/Mid – null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween – substring extraction relative to other strings
  • Split/Join – splits a String into an array of substrings and vice versa
  • Remove/Delete – removes part of a String
  • Replace/Overlay – Searches a String and replaces one String with another
  • Chomp/Chop – removes the last part of a String
  • LeftPad/RightPad/Center/Repeat – pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize – changes the case of a String
  • CountMatches – counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable – checks the characters in a String
  • DefaultString – protects against a null input String
  • Reverse/ReverseDelimited – reverses a String
  • Abbreviate – abbreviates a string using ellipsis
  • Difference – compares two Strings and reports on their differences
  • LevensteinDistance – the number of changes needed to change one String into another

E.g.

// StringUtils
System.out.println(StringUtils.capitalize("abc")) ;
System.out.println(StringUtils.uncapitalize("ABC"));
System.out.println(StringUtils.abbreviate("Monday", 4));
System.out.println(StringUtils.center("abc", 7, null));


System.out.println(StringUtils.isAlpha("a"));
System.out.println(StringUtils.isAlphanumeric("hkHKHik"));
System.out.println(StringUtils.isWhitespace(" "));
System.out.println(StringUtils.isAlphaSpace("xxx yy"));
System.out.println(StringUtils.isAlphanumericSpace("hkHKHikkhbkuh"));
System.out.println(StringUtils.isAsciiPrintable("A"));
System.out.println(StringUtils.isNumeric("23"));
System.out.println(StringUtils.isNumericSpace("23 "));

System.out.println(StringUtils.isEmpty(""));
System.out.println(StringUtils.trimToNull(" \t\r\n\b "));
System.out.println(StringUtils.trimToEmpty(" \t\r\n\b "));
System.out.println(StringUtils.strip("        "));
System.out.println(StringUtils.isBlank("  foo  "));


The output

Abc
aBC
M...
  abc  
true
true
true
true
true
true
true
true
true
null


false

StringEscapeUtils escapes and unescapes Strings for Java, Java Script, HTML, XML, and SQL.
E.g.

// StringEscapeUtils
System.out.println(
  StringEscapeUtils.escapeJava("He didn't say, \"stop!\""));
System.out.println(StringEscapeUtils.escapeHtml("bread & butter"););
System.out.println(StringEscapeUtils.escapeSql("i don't know")););

The output

He didn't say, \"stop!\"
bread & ; butter
i don''t know

RandomStringUtils defines operations for random Strings.
E.g.

// RandomStringUtils
System.out.println(RandomStringUtils.random(20));
System.out.println(RandomStringUtils.randomAscii(20));
System.out.println(RandomStringUtils.randomAlphabetic(20));
System.out.println(RandomStringUtils.randomAlphanumeric(20));
System.out.println(RandomStringUtils.randomNumeric(20));

The output

.....
Blxuk4xp%Zw49p$Y)eg9
lTcRpzniwpLhycEWdfSf
fls2ojJiz4bFPrFvMKQP
30411987433704211638

WordUtils defines perations on Strings that contain words.
E.g.

// WordUtils
String input = "Wrapped after 5 columns.";
System.out.println(WordUtils.wrap(input, 5));
System.out.println(WordUtils.capitalize("I Am Here 123"));
System.out.println(WordUtils.capitalizeFully("I AM HERE 123"));

char[] chars = new char[]{'-', '+', ' ', '@'};
System.out.println(WordUtils.uncapitalize("i-am+HERE 123", chars));

System.out.println(WordUtils.initials("Ben J.Lee"));

System.out.println(WordUtils.swapCase("i am HERE 123"));

The output

Wrapped
after
5
columns.
I Am Here 123
I Am Here 123
i-am+hERE 123
BJ
I AM here 123

Next in the series include

  • Character handling – CharSetUtils, CharSet, CharRange, CharUtils
  • JVM interaction – SystemUtils, CharEncoding
  • Serialization – SerializationUtils, SerializationException
  • Assorted functions – ObjectUtils, ClassUtils, ArrayUtils, BooleanUtils
  • lang.math, lang.time, lang.text


Trackback URL


Sorry, comments for this entry are closed at this time.