Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
charutils
communitycreator

What is CharUtils.toCharacterObject() in Java?

abhilash

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-lang package, 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 null if the input string is null.
  • The method returns null if 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 2 as 2 is the first character of the given string.

  • string - “a23A”

    The method will return a since a is the first character of the given string.

  • string - “A23A”

    The method will return A since A is 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 null since the given string is empty.

  • string - null

    The method will return null since the given string points to a null reference.

RELATED TAGS

java
charutils
communitycreator
RELATED COURSES

View all Courses

Keep Exploring