What is CharUtils.toCharacterObject() in Java?
The toCharacterObject is a static method of the CharUtils class that converts a string to a character object. The given string’s first character is returned.
How to import CharUtils
CharUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-langpackage, refer to the Maven Repository.
You can import the CharUtils class as follows:
import org.apache.commons.lang3.CharUtils;
Syntax
The syntax of the toCharacterObject() method is as follows:
public static Character toCharacterObject(final String str)
Parameters
The toCharacterObject() method takes the following parameter:
final String str: the string to be converted into a character.
Return value
The function returns the first character of the string.
- The method returns
nullif the input string isnull. - The method returns
nullif the input string is empty.
Code
The code below shows how the toCharacterObject() works in Java:
import org.apache.commons.lang3.CharUtils;public class Main {public static void main(String[] args) {String s = "23A";System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));System.out.println();s = "a23A";System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));System.out.println();s = "A23A";System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));System.out.println();s = "~23A";System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));System.out.println();s = "";System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));System.out.println();s = null;System.out.printf("The output of CharUtils.toCharacterObject() for the string - '%s' is %s", s, CharUtils.toCharacterObject(s));}}
Expected output
The output of CharUtils. toCharacterObject() for the string - '23A' is 2
The output of CharUtils. toCharacterObject() for the string - 'a23A' is a
The output of CharUtils. toCharacterObject() for the string - 'A23A' is A
The output of CharUtils. toCharacterObject() for the string - '~23A' is ~
The output of CharUtils. toCharacterObject() for the string - '' is null
The output of CharUtils. toCharacterObject() for the string - 'null' is null
Explanation
The output of the toCharacterObject() method depends on the provided parameter, as described below:
-
string - “23A”
The method will return
2as2is the first character of the given string. -
string - “a23A”
The method will return
asinceais the first character of the given string. -
string - “A23A”
The method will return
AsinceAis the first character of the given string. -
string - “~23A”
The method will return
~since~is the first character of the given string. -
string - “”
The method will return
nullsince the given string is empty. -
string -
nullThe method will return
nullsince the given string points to anullreference.