Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

charutils
java
communitycreator

What is CharUtils.isAsciiPrintable in Java?

abhilash

Overview

The isAsciiPrintable is a static method of the CharUtils class that checks whether the input character is an ASCII printable character.

What are ASCII printable characters?

ASCII printable characters are ASCII characters whose decimal value ranges from 32 (inclusive) to 127 (exclusive).

The following table lists all the printable ASCII characters with their decimal, hexadecimal, and octal values:

Character Decimal Value Hexadecimal Value Octal Value
Space (" ") 32 20 040
! 33 21 041
" 34 22 042
# 35 23 043
$ 36 24 044
% 37 25 045
& 38 26 046
39 27 047
( 40 28 050
) 41 29 051
* 42 2a 052
+ 43 2b 053
, 44 2c 054
- 45 2d 055
. 46 2e 056
/ 47 2f 057
0 48 30 060
1 49 31 061
2 50 32 062
3 51 33 063
4 52 34 064
5 53 35 065
6 54 36 066
7 55 37 067
8 56 38 070
9 57 39 071
: 58 3a 072
; 59 3b 073
< 60 3c 074
= 61 3d 075
> 62 3e 076
? 63 3f 077
@ 64 40 100
A 65 41 101
B 66 42 102
C 67 43 103
D 68 44 104
E 69 45 105
F 70 46 106
G 71 47 107
H 72 48 110
I 73 49 111
J 74 4a 112
K 75 4b 113
L 76 4c 114
M 77 4d 115
N 78 4e 116
O 79 4f 117
P 80 50 120
Q 81 51 121
R 82 52 122
S 83 53 123
T 84 54 124
U 85 55 125
V 86 56 126
W 87 57 127
X 88 58 130
Y 89 59 131
Z 90 5a 132
[ 91 5b 133
\ 92 5c 134
] 93 5d 135
^ 94 5e 136
_ 95 5f 137
` 96 60 140
a 97 61 141
b 98 62 142
c 99 63 143
d 100 64 144
e 101 65 145
f 102 66 146
g 103 67 147
h 104 68 150
i 105 69 151
j 106 6a 152
k 107 6b 153
l 108 6c 154
m 109 6d 155
n 110 6e 156
o 111 6f 157
p 112 70 160
q 113 71 161
r 114 72 162
s 115 73 163
t 116 74 164
u 117 75 165
v 118 76 166
w 119 77 167
x 120 78 170
y 121 79 171
z 122 7a 172
{ 123 7b 173
| 124 7c 174
} 125 7d 175
~ 126 7e 176

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 isAsciiPrintable(final char ch)

Parameters

  • final char ch: Character to check

Return value

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

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

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

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

Explanation

  • Character: ‘F’

    The method returns true because the character is a printable ASCII character and the numerical value of the character is in the range of 0 to 126.

  • Character: ‘\n’

    The method returns true because the character is a printable ASCII character and the numerical value of the character is in the range of 0 to 126.

  • Character: ‘\r’

    The method returns false because the character is not a printable ASCII character and the numerical value of the character is not in the range of 0 to 126.

  • Character: ‘È’

    The method returns false because the character is not a printable ASCII character and the numerical value of the character is not in the range of 0 to 126.

Expected output


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

RELATED TAGS

charutils
java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring