org.apache.commons.lang3.CharUtils Example
In this example we are going to elaborate the use of the CharUtils
class in the package: org.apache.commons.lang3
or previously org.apache.commons.lang
, as the package name says, it is a member of the Apache Commons Lang, and deals with Character (char) 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 char 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. CharUtils class overview, Fields & Methods
The CharUtils
class is really very small but hugely usefull. First of all let me clarify one thing, there are two CharUtils
class in Apache Commons, 1. org.apache.commons.lang.CharUtils
(Commons Lang 2.x API) and 2. org.apache.commons.lang3.CharUtils
(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 CharUtils
class are static so it is never required to create an object or to use the constructor of the CharUtils
class in standard programming, rather you will use it by the class name and appropiate method names, such as:CharUtils.method1(param)
.
1.1 CharUtils Fields
static char CR
: This field contains the Carriage Return (‘\r’) characterstatic char LF
: This fiels contains the Line Feed (‘\n’)character.
1.2 CharUtils Method Summary
CharUtils
handles null
input gracefully. That is to say that a null
input will not cause an Exception in most of the methods. Here we will discuss about the most important methods found in the org.apache.commons.lang3.CharUtils
class. Then we will gradually move to using them.
static char toChar(String str,char defaultVal)
: This method converts aString
to achar
using the 1st character (i.e. the first character is only considered for conversion), if the String object(str) isnull
then returns the default value specified (defaultVal). Also has a variation to support the Character object.static Character toCharacterObject(String str)
: This method converts aString
to aCharacter
object using the 1st character (i.e. the first character is only considered for conversion), ifnull
or empty String is provided as input String then returnsnull
.static int toIntValue(char ch,int defaultVal)
: This method converts the character to the Integer it represents, returns the specified default value if the character is non-numeric. Also has a variation to support Character objects.static String toString(char ch)
: This method converts the character to a String that contains the one character. Also has a variation to support Character objects.static String unicodeEscaped(char ch)
: This method converts achar
to escaped Unicode format String. This format is the Java source code format.static boolean isAscii(char ch)
: This method checks whether the character is ASCII 7 bit or not. There are also other variations of the isAscii… methods I have used each of them in the usage section
1.3 CharUtils Method Usage
- The
static char toChar(String str,char defaultVal)
method:System.out.println(CharUtils.toChar("Java",'a')); String s=null; System.out.println(CharUtils.toChar(s,'a')); System.out.println(CharUtils.toChar("",'a')); Character ch=new Character('H'); System.out.println(CharUtils.toChar(ch),'l');
Output
J a a H
- The
static Character toCharacterObject(String str)
method:System.out.println(CharUtils.toCharacterObject("Hello")); String s=null; System.out.println(s);
Output
H
- The
static int toIntValue(char ch,int defaultVal)
method:
This method converts the character to the Integer it represents, i.e. if the character is'6'
then the function will return6
. You can escape the 2nd argument but doing so may cause an exception if the character passed is non-numeric.System.out.println(CharUtils.toIntValue('6')); System.out.println(CharUtils.toIntValue('x')); System.out.println(CharUtils.toIntValue('7',19)); System.out.println(CharUtils.toIntValue('x',55));
Output
6 Exception in thread "main" java.lang.IllegalArgumentException 71 55
- The
static String toString(char ch)
method:System.out.println(CharUtils.toString('J'));
Output
J
- The
static String unicodeEscaped(char ch)
method:System.out.println(CharUtils.unicodeEscaped(ch));
Output
\u0061
- The
public static boolean isAscii(char ch)
method:System.out.println(CharUtils.isAscii('\n')); System.out.println(CharUtils.isAscii('©')); System.out.println(CharUtils.isAscii('3'));
Output
true false true
- The
public static boolean isAsciiPrintable(char ch)
method:System.out.println(CharUtils.isAsciiPrintable('\n')); System.out.println(CharUtils.isAsciiPrintable('©')); System.out.println(CharUtils.isAsciiPrintable('3'));
Output
false false true
- The
public static boolean isAsciiControl(char ch)
method:System.out.println(CharUtils.isAsciiControl('\n')); System.out.println(CharUtils.isAsciiControl('©')); System.out.println(CharUtils.isAsciiControl('3'));
Output
true false false
- The
public static boolean isAsciiAlpha(char ch)
method:System.out.println(CharUtils.isAsciiAlpha('\n')); System.out.println(CharUtils.isAsciiAlpha('©')); System.out.println(CharUtils.isAsciiAlpha('3')); System.out.println(CharUtils.isAsciiAlpha('a'));
Output
false false false true
- The
public static boolean isAsciiNumeric(char ch)
method:System.out.println(CharUtils.isAsciiNumeric('\n')); System.out.println(CharUtils.isAsciiNumeric('©')); System.out.println(CharUtils.isAsciiNumeric('3')); System.out.println(CharUtils.isAsciiNumeric('a'));
Output
false false true false
2. CharUtils Example
The complete CharUtilsExample.java
package com.javacodegeeks.examples.charutils; import org.apache.commons.lang3.CharUtils; public class CharUtilsExample { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(CharUtils.toChar("Java Programming",'p')); String s=null; System.out.println(CharUtils.toChar(s,'a')); System.out.println(CharUtils.toChar("",'a')); Character ch=new Character('H'); System.out.println(CharUtils.toChar(ch,'l')); System.out.println(CharUtils.toCharacterObject("Hello")); System.out.println(s); System.out.println(CharUtils.toIntValue('6')); System.out.println(CharUtils.toIntValue('7',19)); System.out.println(CharUtils.toIntValue('x',55)); s=CharUtils.toString('J'); System.out.println(s); System.out.println(CharUtils.unicodeEscaped(ch)); System.out.println(CharUtils.isAscii('\n')); System.out.println(CharUtils.isAscii('©')); System.out.println(CharUtils.isAscii('3')); System.out.println(CharUtils.isAsciiPrintable('\n')); System.out.println(CharUtils.isAsciiPrintable('©')); System.out.println(CharUtils.isAsciiPrintable('3')); System.out.println(CharUtils.isAsciiControl('\n')); System.out.println(CharUtils.isAsciiControl('©')); System.out.println(CharUtils.isAsciiControl('3')); System.out.println(CharUtils.isAsciiAlpha('\n')); System.out.println(CharUtils.isAsciiAlpha('©')); System.out.println(CharUtils.isAsciiAlpha('3')); System.out.println(CharUtils.isAsciiAlpha('a')); System.out.println(CharUtils.isAsciiNumeric('\n')); System.out.println(CharUtils.isAsciiNumeric('©')); System.out.println(CharUtils.isAsciiNumeric('3')); System.out.println(CharUtils.isAsciiNumeric('a')); } }
Output
J a a H H null 6 7 55 J \u0048 true false true false false true true false false false false false true
3. Download the Example
This was an example for CharUtils
in Apache Commons lang3.
You can download the full source code of this example here: CharUtilsExample.zip
Thanks for sharing this.