Trusted answers to developer questions

What is StringUtils.isAsciiPrintable in Java?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

isAsciiPrintable() is a static method of the StringUtils class that is used to check if the given string contains only ASCII characters that are printable.

  • The method returns false if the input string is null.
  • The method returns true if the input string is empty.

What are ASCII printable characters?

ASCII printable characters are ASCII characters whose decimal value is in the range 32 to 127.

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 StringUtils

StringUtils 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 StringUtils class as follows.

import org.apache.commons.lang3.StringUtils;

Syntax

public static boolean isAsciiPrintable(final CharSequence cs)

Parameters

  • final CharSequence cs: the character sequence/string to check.

Return value

This method returns true if all the characters in the string are in the range 32 to 126 else returns false.

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "543234 asfg";
System.out.printf("The output of StringUtils.isAsciiPrintable() for the string - '%s' is %s", s, StringUtils.isAsciiPrintable(s));
System.out.println();
s = "";
System.out.printf("The output of StringUtils.isAsciiPrintable() for the string - '%s' is %s", s, StringUtils.isAsciiPrintable(s));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.isAsciiPrintable() for the string - '%s' is %s", s, StringUtils.isAsciiPrintable(s));
System.out.println();
s = " ";
System.out.printf("The output of StringUtils.isAsciiPrintable() for the string - '%s' is %s", s, StringUtils.isAsciiPrintable(s));
System.out.println();
s = "\u008f\u0045";
System.out.printf("The output of StringUtils.isAsciiPrintable() for the string - '%s' is %s", s, StringUtils.isAsciiPrintable(s));
System.out.println();
}
}

Output

The output of the code will be as follows:

The output of StringUtils.isAsciiPrintable() for the string - '543234 asfg' is true
The output of StringUtils.isAsciiPrintable() for the string - '' is true
The output of StringUtils.isAsciiPrintable() for the string - 'null' is false
The output of StringUtils.isAsciiPrintable() for the string - '   ' is true
The output of StringUtils.isAsciiPrintable() for the string - 'E' is false

Explanation

Example 1

  • string - "543234 asfg"

The method returns true, as the string contains only ASCII printable characters.

Example 2

  • string - ""

The method returns true, as the string is empty.

Example 3

  • string - null

The method returns false, as the string points to null reference.

Example 4

  • string - " "

The method returns true, as the string contains only spaces and spaces are ASCII printable characters.

Example 5

  • string - "\u002f\u0045"

The method returns false, as the string contains Unicode characters that are non-ASCII printable characters.

RELATED TAGS

java
stringutils
Did you find this helpful?