Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
ascii
communitycreator

What is CharUtils.isAsciiControl in Java?

abhilash

isAsciiControl is a static method of the CharUtils class that is used to check if the input character is an ASCII control character or not.

What are ASCII control characters?

ASCII control characters are characters whose ASCII value is in the range of 0 to 31. The table below lists the ASCII control characters.

ASCII Value Symbol Description
0 NUL Null
1 SOH Start of heading
2 STX Start of text
3 ETX End of text
4 EOT End of transmission
5 ENQ Enquiry
6 ACK Acknowledge
7 BEL Bell (audible or attention signal)
8 BS Backspace
9 TAB Horizontal tabulation
10 LF Line feed
11 VT Vertical tabulation
12 FF Form feed
13 CR Carriage return
14 SO Shift out
15 SI Shift in
16 DLE Data link rscape
17 DC1 Device control 1
18 DC2 Device control 2
19 DC3 Device control 3
20 DC4 Device control 4
21 NAK Negative acknowledge
22 SYN Synchronous idle
23 ETB End of transmission block
24 CAN Cancel
25 EM End of medium
26 SUB Substitute
27 ESC Escape
28 FS File separator
29 GS Group separator
30 RS Record separator
31 US Unit separator

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.

We can import the CharUtils class as follows.

import org.apache.commons.lang3.CharUtils;

Syntax


public static boolean isAsciiControl(final char ch)

Parameters

final char ch: the character to check.

Return value

The function returns true if the character is an ASCII control 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.isAsciiControl() for the character '%s' is %s", c1, CharUtils.isAsciiControl(c1));
        System.out.println();

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

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

Explanation

  • character - F

    The method returns false, as the character is not an ASCII control character and the numerical value of the character is not in the range of 0 to 31.

  • character - \n

    The method returns true, as the character is an ASCII control character and the numerical value of the character is in the range of 0 to 31.

  • character - \r

    The method returns true, as the character is an ASCII control character and the numerical value of the character is in the range of 0 to 31.

  • character - È

    The method returns false, as the character is not an ASCII control character and the numerical value of the character is not in the range of 0 to 31.

Expected output


The output of CharUtils.isAsciiControl() for the character 'F' is false
The output of CharUtils.isAsciiControl() for the character '
' is true
The output of CharUtils.isAsciiControl() for the character '
' is true
The output of CharUtils.isAsciiControl() for the character 'È' is false

RELATED TAGS

java
ascii
communitycreator
RELATED COURSES

View all Courses

Keep Exploring