org.apache.commons.lang3.StringUtils Example
In this example we are going to elaborate the use of the StringUtils
class in the package: org.apache.commons.lang3.StringUtils
or previously org.apache.commons.lang.StringUtils
, as the package name says, it is a member of the Apache Commons Lang, and deals with String Manipulation. As like other classes of the Apache Commons This class also provides us with some really helpful methods. The methods of this class as other classes of Apache Commons are wrappers for very important tasks regarding String Manipulation so the code which implemented those methods becomes significantly smaller, cleaner and understandable comparing to other programs where those functionalities are written manually.
1. The StringUtils class overview, Fields & Methods.
First of all let me clarify one thing, there are two StringUtils
class in Apache Commons, 1. org.apache.commons.lang.StringUtils
(Commons Lang 2.x API) and 2. org.apache.commons.lang3.StrinUtils
(Commons Lang 3.1 API and later). Here we will discuss the second one as it is the updated version. All the member methods of the StringUtils
class are static so it is never required to create an object or to use the constructor of the StringUtils
class in standard programming, rather you will use it by the class name and appropiate method names, such as: StringUtils.method1(param)
. Some methods behavior of org.apache.commons.lang3.StringUtils
class slightly differs from the org.apache.commons.lang.StringUtils
class and also some new fields are included in the org.apache.commons.lang3.StringUtils
class. I will note them wherever applicable.
The StringUtils
class defines some words related to String handling.
null
:null
empty
: A zero-length string (""
)space
: The space character (' '
, char 32)whitespace
: The characters that are defined byCharacter.isWhitespace(char)
trim
: The characters <= 32 as inString.trim()
1.1 The StringUtils Fields
static String CR
: This field contains a field for Carriage Return (“\r”), Was not inlang.StringUtils
.static String EMPTY
: This field contains the Empty (“”) String.static int INDEX_NOT_FOUND
: This field represents a failed index search.static String LF
: This field contains a String for linefeed (“\n”), Was not inlang.StringUtils
.static String SPACE
: This field contains a String for a space character, Was not inlang.StringUtils
.
1.2 StringUtils Method Summary
StringUtils
handles null
input Strings quietly. That is to say that a null
input will return null
, so there should never be any NullPointerException
.Here we will discuss about the most important methods found in the org.apache.commons.lang3.StringUtils
class. Then we will gradually move to using them.
static String abbreviate(String str,int offset,int maxWidth)
: This method abbreviates the String provided using ellipses, the offset parameter is optional. Throws anIllegalArgumentException
if the maxWidth is lesser than minimum specified (varies with providing the offset or not).static String abbreviateMiddle(String str,String middle,int length)
: This method abbreviates a String to the length replacing the middle characters with the supplied replacement String.static String appendIfMissing(String str,CharSequence suffix,CharSequence... suffixes)
: This method appends the suffix(2nd parameter) a String(1st Parameter) to the end if the String does not already ends with any of the suffixes (3rd parameter, may be escaped).This method is newly indtroduced in API 3.2 (lang3.StringUtils). Also there is another method:prependIfMissing(String str,CharSequence prefix,CharSequence... prefixes)
to add prefixes to the beginning, this is also a new method in API 3.2static String center(String str,int Size,char padChar)
: This method returns the supplied String after centering it to a larger String of size size, it uses the supplied character (space character if escaped) to pad the String. There are also methods likeleftPad(String str,int size,char padChar)
,rightPad(String str,int size,char padChar)
to pad Strings to left or right.static String chomp(String str)
: This method returns the supplied String after removing one newline character from the end of a String (if it is there). a newline character is : “\n”, “\r”, or “\r\n”.static String chop(String str)
: This method returns the supplied String by simply removing the last character.static boolean contains(CharSequence str,CharSequence searchStr)
: This method check if the CharSequence contains the search CharSequence. Returnstrue
if contains andfalse
if not or if the CharSequence isnull
. There is also a methodcontainsNone(CharSequence cs, char... searchChars)
performs just the opposite thing, i.e. returnstrue
if it does not contain the characters andfalse
if doesstatic String deleteWhitespace(String str)
: This method returns the supplied String after deleting all whitespaces from it.static String difference(String str1,String str2)
: This method compares two Strings provided, and returns the portion from the second String where they differ.static boolean endsWith(CharSequence str,CharSequence suffix)
: This method checks if a charsequence ends with a specified suffix. The comparison is case sensitive. Twonull
references are considered to be equal. There is another methodstatic boolean startsWith(CharSequence str,CharSequence preffix)
to check if a charsequence starts with a specified preffix.static boolean equals(CharSequence cs1,CharSequence cs2)
: This method compares two CharSequences, returningtrue
if they represent equal sequences of characters.static String getCommonPrefix(String... strs)
: This method compares all Strings in an array and returns the initial (in the beginning of the Strings) sequesnce of characters that is common to all of them.static double getJaroWinklerDistance(CharSequence first,CharSequence second)
: This method calculates the Jaro Winkler distance (which indicates the similarity score) between two Strings. The Jaro measure is the weighted sum of percentage of matched characters from each String and transposed characters. Winkler increased this measure for matching initial characters. This implementation is based on the Jaro Winkler similarity algorithm from http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance. This method is newly indtroduced in API 3.3 (lang3.StringUtils).static int getLevenshteinDistance(CharSequence str1,CharSequence str2,int threshold)
: This method Find the Levenshtein (the number of changes needed to change one String into another, where each change is a single character modification) distance between two Strings if it’s less than or equal to a given threshold. The threshold parameter is optional inlang3.StringUtils
and is not available inlang.StringUtils
. This method throws an illegalArgumentException – if either String input nullstatic String removeEnd(String str,String remove)
: This method removes a substring only if it is at the end of a source string, otherwise returns the source string. There is also another method :static String removeStart(String str,String remove)
to remove substring from the beginning.static String String removePattern(String source,String regex)
: This method removes each substring of the source String that matches the given regular expression using the DOTALL option. This method is newly indtroduced in API 3.2 (lang3.StringUtils).static String repeat(String str,String separator,int repeat)
: This method returns the provided String (str) after repeating it repeat times to form a new String, with a String separator (optional) injected each time.static String replace(String text,String searchStr,String replacement,int n)
: This method replaces the searchStr (search String) provided with the replacement, inside the text, for the first n (optional) times.static String reverse(String str)
: This method returns a String after reversing it as per StringBuilder.reverse().static String[] split(String str,String separator,int max)
: This method splits the provided text into an array with a maximum length, separators specified. The separator is not included in the returned String array. Adjacent separators are treated as one separator. A null input String returns null. A null separatorChars splits on whitespace. If more than max delimited substrings are found, the last returned string includes all characters after the first max – 1 returned strings (including separator characters).static String strip(String str,String stripChars)
: Strips any of a set of characters from the start and end of a String. This is similar to String.trim() but allows the characters to be stripped to be controlled. A null input String returns null. An empty string (“”) input returns the empty string. If the stripChars String is null, whitespace is stripped. Alternatively use strip(String).static String swapCase(String str)
: This method returns the supplied String after changing upper and title case to lower case, and lower case to upper case. A null input String returns null.static String trim(String str)
: This method removes control characters (char <= 32) from both ends of this String, handling null by returning null. The String is trimmed using String.trim(). Trim removes start and end characters <= 32, to strip whitespace use strip(String), to trim your choice of characters, use the strip(String, String) methods.
1.3 StringUtils Methods Usage
- The
StringUtils.abbreviate(String str,int offset,int maxWidth)
method
The offset parameter is optional. The maxWidth parameter must be atleast 4 if the offset parameter is not provided and must be atleast 7 if provided.Here are some coding examples:System.out.println(StringUtils.abbreviate(null, 1)); System.out.println(StringUtils.abbreviate("", 4)); System.out.println(StringUtils.abbreviate("abcdefg", 6)); System.out.println(StringUtils.abbreviate("abcdefg", 7)); System.out.println(StringUtils.abbreviate("abcdefg", 8)); System.out.println(StringUtils.abbreviate("abcdefg", 4)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", -1, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 0, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 1, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 4, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 5, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 6, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 8, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 10, 10)); System.out.println(StringUtils.abbreviate("abcdefghijklmno", 12, 10));
Output:-
null abc... abcdefg abcdefg a... abcdefg... abcdefg... abcdefg... abcdefg... ...fghi... ...ghij... ...ijklmno ...ijklmno ...ijklmno
This two codes will result in an Exception:
System.out.println(StringUtils.abbreviate("abcdefghij", 5, 6)); System.out.println(StringUtils.abbreviate("abcdefg", 3));
- The
StringUtils.abbreviateMiddle(String str,String middle,int length)
method
This abbreviation only occurs if the following criteria is met:- Neither the String for abbreviation nor the replacement String are null or empty
- The length to truncate to is less than the length of the supplied String
- The length to truncate to is greater than 0
- The abbreviated String will have enough room for the length supplied replacement String and the first and last characters of the supplied String for abbreviation
Otherwise, the returned String will be the same as the supplied String for abbreviation.
System.out.println(StringUtils.abbreviateMiddle("This is Java", "**", 4));
Output
T**a
- The
StringUtils.appendIfMissing(String str,CharSequence suffix,CharSequence... suffixes)
methodSystem.out.println(StringUtils.appendIfMissing("abc", "xyz", "mno")); System.out.println(StringUtils.appendIfMissing("abcxyz", "xyz", "mno")); System.out.println(StringUtils.appendIfMissing("abcmno", "xyz", "mno")); System.out.println(StringUtils.appendIfMissing("abcXYZ", "xyz", "mno"));
Output
abcxyz abcxyz abcmno abcXYZxyz
As you can see This method is case sensitive. There is another method
StringUtils.appendIfMissingIgnoreCase(String str,CharSequence suffix,CharSequence... suffixes)
which overcomes this limitation. - The
StringUtils.center(String str,int Size,char padChar)
methodSystem.out.println(StringUtils.center(null, 2)); System.out.println(StringUtils.center("ab", -1)); System.out.println(StringUtils.center("ab", 4)); System.out.println(StringUtils.center("abcd", 2)); System.out.println(StringUtils.center("a", 4)); System.out.println(StringUtils.center("", 4, '-')); System.out.println(StringUtils.center("ab", -1, '-')); System.out.println(StringUtils.center("ab", 4, ' ')); System.out.println(StringUtils.center("abcd", 2, ' ')); System.out.println(StringUtils.center("a", 4, '-')); System.out.println(StringUtils.center("a", 4, 'y'));
Output
null ab ab abcd a ---- ab -ab- abcd -a-- yayy
- The
StringUtils.chop(String str)
methodSystem.out.println(StringUtils.chop("Java Programm!"));
Output
Java Program
- The
StringUtils.deleteWhitespace(String str)
methodSystem.out.println(StringUtils.deleteWhitespace(null)); System.out.println(StringUtils.deleteWhitespace("abc")); System.out.println(StringUtils.deleteWhitespace(" ab c "));
Output
null abc abc
- The
StringUtils.difference(String str1,String str2)
methodSystem.out.println(StringUtils.difference("abcde", "abxyz")); System.out.println(StringUtils.difference("abxyz", "abcde"));
Output
xyz cde
- The
StringUtils.getCommonPrefix(String... strs)
methodSystem.out.println(StringUtils.getCommonPrefix("i am a machine", "i am a robot","I am s Human"));
Output
I am a
- The
StringUtils.getJaroWinklerDistance(CharSequence first,CharSequence second)
methodSystem.out.println(StringUtils.getJaroWinklerDistance("","")); System.out.println(StringUtils.getJaroWinklerDistance("","a")); System.out.println(StringUtils.getJaroWinklerDistance("aaapppp", "")); System.out.println(StringUtils.getJaroWinklerDistance("frog", "fog")); System.out.println(StringUtils.getJaroWinklerDistance("fly", "ant")); System.out.println(StringUtils.getJaroWinklerDistance("elephant", "hippo")); System.out.println(StringUtils.getJaroWinklerDistance("hippo", "elephant")); System.out.println(StringUtils.getJaroWinklerDistance("hippo", "zzzzzzzz")); System.out.println(StringUtils.getJaroWinklerDistance("hello", "hallo")); System.out.println(StringUtils.getJaroWinklerDistance("ABC Corporation", "ABC Corp")); System.out.println(StringUtils.getJaroWinklerDistance("D N H Enterprises Inc", "D & H Enterprises, Inc.")); System.out.println(StringUtils.getJaroWinklerDistance("My Gym Children's Fitness Center", "My Gym. Childrens Fitness")); System.out.println(StringUtils.getJaroWinklerDistance("PENNSYLVANIA", "PENNCISYLVNIA")); System.out.println(StringUtils.getJaroWinklerDistance("PENNSYLVANIA", "PENNSYLVANIA"));
Output
0.0 0.0 0.0 0.93 0.0 0.44 0.44 0.0 0.88 0.91 0.93 0.94 0.9 1.0
- The
StringUtils.getLevenshteinDistance(CharSequence str1,CharSequence str2,int threshold)
methodSystem.out.println(StringUtils.getLevenshteinDistance("","")); System.out.println(StringUtils.getLevenshteinDistance("","a")); System.out.println(StringUtils.getLevenshteinDistance("aaapppp", "")); System.out.println(StringUtils.getLevenshteinDistance("frog", "fog")); System.out.println(StringUtils.getLevenshteinDistance("fly", "ant")); System.out.println(StringUtils.getLevenshteinDistance("elephant", "hippo")); System.out.println(StringUtils.getLevenshteinDistance("hippo", "elephant")); System.out.println(StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz")); System.out.println(StringUtils.getLevenshteinDistance("hello", "hallo")); System.out.println(StringUtils.getLevenshteinDistance("aaapppp", "", 8)); System.out.println(StringUtils.getLevenshteinDistance("aaapppp", "", 7)); System.out.println(StringUtils.getLevenshteinDistance("aaapppp", "", 6)); System.out.println(StringUtils.getLevenshteinDistance("elephant", "hippo", 7)); System.out.println(StringUtils.getLevenshteinDistance("elephant", "hippo", 6)); System.out.println(StringUtils.getLevenshteinDistance("hippo", "elephant", 7)); System.out.println(StringUtils.getLevenshteinDistance("hippo", "elephant", 6));
Output
0 1 7 1 3 7 7 8 1 7 7 -1 7 -1 7 -1
- The
StringUtils.removeEnd(String str,String remove)
methodSystem.out.println(StringUtils.removeEnd("This is Java Code Example","Example"));
Output
This is Java Code
- The
StringUtils.repeat(String str,String separator,int repeat)
methodSystem.out.println(StringUtils.repeat("Rivu","-",6));
Output
Rivu-Rivu-Rivu-Rivu-Rivu-Rivu
- The
StringUtils.replace(String text,String searchStr,String replacement,int n)
method
The n parameter is optional, passing -1 in the n would be similar to not providing the n.System.out.println(StringUtils.replace(null,null,null)); System.out.println(StringUtils.replace("any", null, "")); System.out.println(StringUtils.replace("any", "", null)); System.out.println(StringUtils.replace("any", "", null)); System.out.println(StringUtils.replace("aba", "a", null)); System.out.println(StringUtils.replace("aba", "a", "")); System.out.println(StringUtils.replace("aba", "a", "z")); System.out.println(StringUtils.replace("any", null, "", 3)); System.out.println(StringUtils.replace("any", "", null, 10)); System.out.println(StringUtils.replace("any", "", "", 3)); System.out.println(StringUtils.replace("any", "a", "s", 0)); System.out.println(StringUtils.replace("abaa", "a", null, -1)); System.out.println(StringUtils.replace("abaa", "a", "", -1)); System.out.println(StringUtils.replace("abaa", "a", "z", 0)); System.out.println(StringUtils.replace("abaa", "a", "z", 1)); System.out.println(StringUtils.replace("abaa", "a", "z", 2)); System.out.println(StringUtils.replace("abaa", "a", "z", -1));
Output
null any any any aba b zbz any any any any abaa b abaa zbaa zbza zbzz
- The
StringUtils.reverse(String str)
methodSystem.out.println(StringUtils.reverse("Java"));
Output
avaJ
- The
StringUtils.split(String str,String separator,int max)
method
The max parameter is optionalString str="It is Java Code Geeks"; System.out.println(str); System.out.println("Splitting str:-"); String[] strs=StringUtils.split(str," "); for(String s:strs) { System.out.println(s); }
Output
It is Java Code Geeks
- The
StringUtils.strip(String str,String stripChars)
String xx="java to java"; System.out.println("before strip : "+xx); String s=StringUtils.strip(xx, "aj"); System.out.println("After strip : "+s);
Output
before strip : java to java After strip : va to jav
- The
StringUtils.swapCase(String str)
methodSystem.out.println(StringUtils.swapCase("This is Funny"));
Output
tHIS IS fUNNY
- The
StringUtils.isAllLowerCase(String str)
method
static boolean isAllLowerCase(String str)
returnstrue
if all the containings of the passed String is in lower case.System.out.println(StringUtils.isAllLowerCase("this will return true")); System.out.println(StringUtils.isAllLowerCase("this will returN false"));
Output
true false
- The
StringUtils.isAllUpperCase(String str)
method
static boolean isAllUpperCase(String str)
returnstrue
if all the containings of the passed String is in upper case.System.out.println(StringUtils.isAllUpperCase("THIS WILL RETURN TRUE")); System.out.println(StringUtils.isAllUpperCase("THIS will returN false"));
Output
true false
- The
StringUtils.isAlpha(String str)
method
static boolean isAlpha(String str)
returnstrue
if the passed String contains only Unicode letters.System.out.println(StringUtils.isAlpha("abc")); System.out.println(StringUtils.isAlpha("a 2bc"));
Output
true false
- The
StringUtils.isAlphaSpace(String str)
method
static boolean isAlphaSpace(String str)
returnstrue
if the passed String contains only Unicode letters and space (‘ ‘).System.out.println(StringUtils.isAlphaSpace("ab c")); System.out.println(StringUtils.isAlphaSpace("a 2bc"));
Output
true false
- The
StringUtils.isAlphanumeric(String str)
method
static boolean isAlphanumeric(String str)
returnstrue
if the passed String contains only Unicode letters or digits.System.out.println(StringUtils.isAlphanumeric("ab2c")); System.out.println(StringUtils.isAlphanumeric("a 2bc"));
Output
true false
- The
StringUtils.isAlphanumericSpace(String str)
method
static boolean isAlphanumericSpace(String str)
returnstrue
if the passed String contains only Unicode letters, digits or space (‘ ‘).System.out.println(StringUtils.isAlphanumericSpace("ab2c")); System.out.println(StringUtils.isAlphanumericSpace("a 2bc")); System.out.println(StringUtils.isAlphanumericSpace("a-2bc"));
Output
true true false
- The
StringUtils.isAsciiPrintable(String str)
method
static boolean isAsciiPrintable(String str)
returnstrue
if the passed String contains only ASCII printable characters.System.out.println(StringUtils.isAsciiPrintable("!ab-c~")); System.out.println(StringUtils.isAsciiPrintable("Ceki G�lc�"));
Output
true false
2. StringUtils Example
The complete StringUtilsExample.java
package stringutilsexample; import java.io.PrintWriter; import java.util.Random; import org.apache.commons.lang3.StringUtils; public class StringUtilsExample { public static void main(String[] args) { String str="love Java Programm",str2="jAva"; System.out.println("Using the StringUtils Class"); //Using StringUtils.abbreviate() System.out.println("Using StringUtils.abbreviate()"); System.out.println("str = "+str); String abstr=StringUtils.abbreviate(str, 14); System.out.println("After abbreviation abstr="+abstr); abstr=StringUtils.abbreviate(str, 7, 14); System.out.println("After abbreviation abstr="+abstr); //Using StringUtils.abbreviateMiddle() abstr=StringUtils.abbreviateMiddle(str, "...", 16); System.out.println("After abbreviation abstr="+abstr); //Using StringUtils.appendIfMissingIgnoreCase() str=StringUtils.appendIfMissingIgnoreCase(str, "ing", "ing","s"); System.out.println("After appending str becomes : "+str); //Using StringUtils.prependIfMissingIgnoreCase() str=StringUtils.prependIfMissingIgnoreCase(str, "Rivu ", "Rivu","I"); System.out.println("After prepending str becomes : "+str); //Using StringUtils.reverse() System.out.println("Reverse str : "+StringUtils.reverse(str)); //Using StringUtils.capitalize() System.out.println("str2 = "+str2); str2=StringUtils.capitalize(str2); System.out.println("After capitalize : "+str2); //Using StringUtils.uncapitalize() System.out.println("str2 = "+str2); str2=StringUtils.uncapitalize(str2); System.out.println("After uncapitalize : "+str2); //Using StringUtils.swapCase() System.out.println("str2 = "+str2); str2=StringUtils.swapCase(str2); System.out.println("After swap case : "+str2); //Using StringUtils.lowerCase() System.out.println("str2 = "+str2); str2=StringUtils.lowerCase(str2); System.out.println("After lower case : "+str2); //Using StringUtils.lowerCase() System.out.println("str2 = "+str2); str2=StringUtils.upperCase(str2); System.out.println("After upper case : "+str2); //Using StringUtils.center() System.out.println("str2 = "+str2); String cstr=StringUtils.center(str2, 10, '-'); System.out.println("After centerring : "+cstr); //Using StringUtils.chomp() str2=str2+"\n"; System.out.println("str2 ="+str2+"With new Line"); str2=StringUtils.chomp(str2); System.out.println("Now str2 = "+str2+" After chomp"); //Using StringUtils.contains() if(StringUtils.containsIgnoreCase(str, "JAVA")) System.out.println(str+" is about Java"); else System.out.println(str+" is not about Java"); //Using StringUtils.deleteWhiteSpace() System.out.println("str = "+str); String newstr=StringUtils.deleteWhitespace(str); System.out.println("newstr = "+newstr); //Using StringUtils.chop() System.out.println("cstr ="+cstr); cstr=StringUtils.chop(cstr); System.out.println(" After chop : "+cstr); //Using StringUtils.endsWithIgnoreCase() if(StringUtils.endsWithIgnoreCase(str, "ing")) System.out.println(str+" ends with a \"ing\""); else System.out.println(str+" does not ends with a \"ing\""); //Using StringUtils.endsWithIgnoreCase() if(StringUtils.startsWithIgnoreCase(str, "rivu")) System.out.println(str+" starts with a \"rivu\""); else System.out.println(str+" does not starts with \"Rivu\""); String x="This is a String",x1="This is String 1",x2="This is String 2",x3="This is a String"; System.out.println("x = "+x); System.out.println("x1 = "+x1); System.out.println("x2 = "+x2); System.out.println("x2 = "+x3); //using StringUtils.equals() if(StringUtils.equals(x1, x2)) System.out.println("x1 and x2 are equal"); else System.out.println("x1 and x2 are not equal"); if(StringUtils.equalsIgnoreCase(x, x3)) System.out.println("x and x3 are equal"); else System.out.println("x and x3 are not equal"); //using StringUtils.difference() String d1=StringUtils.difference(x, x1); System.out.println("Difference between x and x1 : "+d1); String d2=StringUtils.difference(x1, x2); System.out.println("Difference between x1 and x2 : "+d2); String d3=StringUtils.difference(x1, x); System.out.println("Difference between x1 and x : "+d3); //using StringUtils.getCommonPrefix() System.out.println("Common Prefix in x,x1,x2,x3 is : "+StringUtils.getCommonPrefix(x,x1,x2,x3)); //using StringUtils.getJaroWinklerDistance() double jw1=StringUtils.getJaroWinklerDistance(x, x1); double jw2=StringUtils.getJaroWinklerDistance(x1, x2); double jw3=StringUtils.getJaroWinklerDistance(x2, x3); double jw4=StringUtils.getJaroWinklerDistance(x, x3); System.out.println("Jaro Winkler Distance between x and x1 : "+jw1); System.out.println("Jaro Winkler Distance between x1 and x2 : "+jw2); System.out.println("Jaro Winkler Distance between x2 and x3 : "+jw3); System.out.println("Jaro Winkler Distance between x and x3 : "+jw4); //using StringUtils.getLevenshteinDistance() int l1=StringUtils.getLevenshteinDistance(x, x1); int l2=StringUtils.getLevenshteinDistance(x1, x2); int l3=StringUtils.getLevenshteinDistance(x2, x3); int l4=StringUtils.getLevenshteinDistance(x, x3); int l5=StringUtils.getLevenshteinDistance(x2, x3, 3); System.out.println("Levenshtein Distance between x and x1 : "+l1); System.out.println("Levenshtein Distance between x1 and x2 : "+l2); System.out.println("Levenshtein Distance between x2 and x3 : "+l3); System.out.println("Levenshtein Distance between x and x3 : "+l4); System.out.println("Levenshtein Distance between x2 and x3 upto threshold 3 : "+l5); //Using StringUtils.isAllLowerCase() System.out.println("isAllLowerCase(str) = "+StringUtils.isAllLowerCase(str)); //Using StringUtils.isAllUpperCase() System.out.println("isAllUpperCase(str) = "+StringUtils.isAllUpperCase(str)); //Using StringUtils.isAlpha() System.out.println("isAlpha(str) = "+StringUtils.isAlpha(str)); //Using StringUtils.isAlphanumeric() System.out.println("isAlphanumeric(str) = "+StringUtils.isAlphanumeric(str)); //Using StringUtils.isAlphanumericSpace() System.out.println("isAlphanumericSpace(str) = "+StringUtils.isAlphanumericSpace(str)); //Using StringUtils.isAlphaSpace() System.out.println("isAlphaSpace(str) = "+StringUtils.isAlphaSpace(str)); //Using StringUtils.isAnyBlank() System.out.println("isAnyBlank(str) = "+StringUtils.isAnyBlank(str)); //Using StringUtils.isAnyEmpty() System.out.println("isAnyEmpty(str) = "+StringUtils.isAnyEmpty(str)); //Using StringUtils.isAsciiPrintable() System.out.println("isAsciiPrintable(str) = "+StringUtils.isAsciiPrintable(str)); //Using StringUtils.isBlank() System.out.println("isBlank(str) = "+StringUtils.isBlank(str)); //Using StringUtils.rightPad() System.out.println("Before Rightpad : "+cstr); String rp=StringUtils.rightPad(cstr, 15, 'x'); System.out.println("After Rightpad : "+rp); //Using StringUtils.replace() rp=StringUtils.replace(rp, "-", "."); System.out.println("After replace "+rp); //Using StringUtils.repeat() rp=StringUtils.repeat(rp, ">", 5); System.out.println("After repeat "+rp); //Using StringUtils.split() System.out.println("Splitting str:-"); String[] strs=StringUtils.split(str," "); for(String s:strs) { System.out.println(s); } //Using StringUtils.strip() String xx="java to java"; System.out.println("before strip : "+xx); String s=StringUtils.strip(xx, "aj"); System.out.println("After strip : "+s); } }
Output
Using the StringUtils Class Using StringUtils.abbreviate() str = love Java Programm After abbreviation abstr=love Java P... After abbreviation abstr=...va Programm After abbreviation abstr=love Ja...ogramm After appending str becomes : love Java Programming After prepending str becomes : Rivu love Java Programming Reverse str : gnimmargorP avaJ evol uviR str2 = jAva After capitalize : JAva str2 = JAva After uncapitalize : jAva str2 = jAva After swap case : JaVA str2 = JaVA After lower case : java str2 = java After upper case : JAVA str2 = JAVA After centerring : ---JAVA--- str2 =JAVA With new Line Now str2 = JAVA After chomp Rivu love Java Programming is about Java str = Rivu love Java Programming newstr = RivuloveJavaProgramming cstr =---JAVA--- After chop : ---JAVA-- Rivu love Java Programming ends with a "ing" Rivu love Java Programming starts with a "rivu" x = This is a String x1 = This is String 1 x2 = This is String 2 x2 = This is a String x1 and x2 are not equal x and x3 are equal Difference between x and x1 : String 1 Difference between x1 and x2 : 2 Difference between x1 and x : a String Common Prefix in x,x1,x2,x3 is : This is Jaro Winkler Distance between x and x1 : 0.94 Jaro Winkler Distance between x1 and x2 : 0.98 Jaro Winkler Distance between x2 and x3 : 0.94 Jaro Winkler Distance between x and x3 : 1.0 Levenshtein Distance between x and x1 : 4 Levenshtein Distance between x1 and x2 : 1 Levenshtein Distance between x2 and x3 : 4 Levenshtein Distance between x and x3 : 0 Levenshtein Distance between x2 and x3 upto threshold 3 : -1 isAllLowerCase(str) = false isAllUpperCase(str) = false isAlpha(str) = false isAlphanumeric(str) = false isAlphanumericSpace(str) = true isAlphaSpace(str) = true isAnyBlank(str) = false isAnyEmpty(str) = false isAsciiPrintable(str) = true isBlank(str) = false Before Rightpad : ---JAVA-- After Rightpad : ---JAVA--xxxxxx After replace ...JAVA..xxxxxx After repeat ...JAVA..xxxxxx>...JAVA..xxxxxx>...JAVA..xxxxxx>...JAVA..xxxxxx>...JAVA..xxxxxx Splitting str:- Rivu love Java Programming before strip : java to java After strip : va to jav
2.1 Conclusion
In this Example I have used most significant methods of the StringUtils class. I have used the ‘IgnoreCase’ variants of the methods (where applicable). I marked the use of every method by comments, so you will easily get that on which portion of the example I am working on which method.
3. Download the Example
This was an example for StringUtils
in Apache Commons lang3.
You can download the full source code of this example here : StringUtilsExample.zip