Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

charutils
java

What is CharUtils.isAscii in Java?

abhilash

The isAscii method is a static method of the CharUtils class that checks whether the input character is a 7-bit ASCII character. The method checks if the character’s decimal value ranges from 0 to 128.

How to import CharUtils

CharUtils is defined in the Apache Commons Lang package. The 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.

We can import the CharUtils class as follows:

import org.apache.commons.lang3.CharUtils;

Syntax


public static boolean isAscii( char ch)

Parameters

  • ch: the character to be checked whether it is an ASCII character

Return value

The function returns true if the character is an ASCII character. Otherwise, it returns false.

Code

import org.apache.commons.lang3.CharUtils;

public class Main {

    public static void main(String[] args) {
        char c1 = 'F';
        System.out.printf("The output of CharUtils.isAscii() for the character '%s' is %s", c1, CharUtils.isAscii(c1));
        System.out.println();

        c1 = 'p';
        System.out.printf("The output of CharUtils.isAscii() for the character '%s' is %s", c1, CharUtils.isAscii(c1));
        System.out.println();

        c1 = '9';
        System.out.printf("The output of CharUtils.isAscii() for the character '%s' is %s", c1, CharUtils.isAscii(c1));
        System.out.println();

        c1 = '@';
        System.out.printf("The output of CharUtils.isAscii() for the character '%s' is %s", c1, CharUtils.isAscii(c1));
        System.out.println();

        c1 = 'È';
        System.out.printf("The output of CharUtils.isAscii() for the character '%s' is %s", c1, CharUtils.isAscii(c1));
        System.out.println();
    }
}

Expected output


The output of CharUtils.isAscii() for the character 'F' is true
The output of CharUtils.isAscii() for the character 'p' is true
The output of CharUtils.isAscii() for the character '9' is true
The output of CharUtils.isAscii() for the character '@' is true
The output of CharUtils.isAscii() for the character 'È' is false

Explanation

  • c1 = 'F'

    The method returns true since the character is an ASCII character and the character’s numerical value is in the 0 to 128 range.

  • c1 = 'p'

    The method returns true since the character is an ASCII character and the character’s numerical value is in the 0 to 128 range.

  • c1 = '9'

The method returns true since the character is an ASCII character and the character’s numerical value is in the 0 to 128 range.

  • c1 = '@'

The method returns true since the character is an ASCII character and the character’s numerical value is in the 0 to 128 range.

  • c1 = 'È'

The method returns false as the character is a UNICODE character, and the character’s numerical value is not in 0 to 128 range.

RELATED TAGS

charutils
java
RELATED COURSES

View all Courses

Keep Exploring