CharUtils

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’) character
  • static 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.CharUtilsclass. Then we will gradually move to using them.

  • static char toChar(String str,char defaultVal) : This method converts a String to a char using the 1st character (i.e. the first character is only considered for conversion), if the String object(str) is null then returns the default value specified (defaultVal). Also has a variation to support the Character object.
  • static Character toCharacterObject(String str) : This method converts a String to a Character object using the 1st character (i.e. the first character is only considered for conversion), if null or empty String is provided as input String then returns null.
  • 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 a char 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

  1. 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
    
  2. The static Character toCharacterObject(String str) method:
    System.out.println(CharUtils.toCharacterObject("Hello"));
    String s=null;
    System.out.println(s);
    

    Output

    H
      
    
  3. 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 return 6. 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
    
  4. The static String toString(char ch) method:
    System.out.println(CharUtils.toString('J'));
    

    Output

    J
    
  5. The static String unicodeEscaped(char ch) method:
    System.out.println(CharUtils.unicodeEscaped(ch));
    

    Output

    \u0061
    
  6. 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
    
  7. 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
    
  8. 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
    
  9. 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
    
  10. 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.

Download
You can download the full source code of this example here: CharUtilsExample.zip

Rivu Chakraborty

Rivu Chakraborty is a Google Certified Android Developer, Sr. Tech Member of Institute of Engineers (India), he also have certifications on Scrum. He is also an author of multiple books on Kotlin, Reactive Programming, Functional Programming and Android Development, published by renowned publication houses. Having total 5+ years of experience he is presently working as a Sr. Software Engineer (Android) at Indus Net Technologies Pvt. Ltd. Rivu Chakraborty considers himself a Kotlin and Android enthusiast and a Kotlin evangelist. He has been using Kotlin since December 2015, so he has around 2 years' experience in Kotlin. As part of his mission to expand the use and knowledge of the Kotlin Language as much as possible, he created the KotlinKolkata User Group, one of the most active Kotlin user groups throughout the world and he is a Founder Organizer of KotlinKolkata. He is also an active member of GDG Kolkata and gives talks at GDG Kolkata Meetups.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Madhavi Ala
Madhavi Ala
4 years ago

Thanks for sharing this.

Back to top button