How to Check if a String contains only ASCII in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report The full form of ASCII is the American Standard Code for Information Interchange. It is the numeric representation of Character. As Java supports multiple languages, and it follows the Unicode system. In simple terms for better understanding, it converts the Character to a specific unique number, and then the number converts to the machine-level code, and the range of ASCII value is 0 to 127, and now we want to check whether a string only contains ASCII characters. We can use String.matches() method and passes a Regex (regular expressions) in it, and it returns a boolean value. Checking If String Contains Only ASCII CharactersSo, we have two strings s1 and s2, s1 contains only ASCII characters and the second string s2 contains some special or non-ASCII characters. Input: // contains ASCII characters s1 = "GeeksforGeeks" // contains non-ASCII character s2 = "€_is_a_Symbol_of_euro" Below is the program for Checking If the String Contains Only ASCII Characters: Java // Java Program for checking a // String contains only ASCII characters. import java.io.*; // Driver Class class GFG { // Main Function public static void main(String[] args) { // contains ASCII characters String s1 = "GeeksforGeeks"; // contains non-ASCII // character String s2 = "€_is_a_Symbol_of_euro"; // printing the output System.out.println( "is s1 contains only ASCII characters :" + s1.matches("\\A\\p{ASCII}*\\z")); System.out.println( "is s2 contains only ASCII characters :" + s2.matches("\\A\\p{ASCII}*\\z")); } } Outputis s1 contains only ASCII characters :true is s2 contains only ASCII characters :false Another Approach We know that the range of ASCII value is between 0 to 127 so if the characters Unicode is not lies in this range so the string contains non-ASCII character. Approach: So first we create a boolean variable and set it's default value true.Because we initially assumes that the string only contains the ASCII characterThen we iterate through each characters and checks it's range. if it not lie in the range 0 to 127 so we set boolean variable's value as false.And print the output on console. Java // Java Program for checking a String // Contains only ASCII characters. import java.io.*; class GFG { public static void main(String[] args) { // contains ASCII characters String s1 = "GeeksforGeeks"; // contains non-ASCII // character String s2 = "€_is_a_Symbol_of_euro"; boolean checkASCII_for_s1 = true; boolean checkASCII_for_s2 = true; for (int i = 0; i < s1.length(); i++) { int Char = s1.charAt(i); if (Char > 127) { checkASCII_for_s1 = false; break; } } for (int i = 0; i < s2.length(); i++) { int Char = s2.charAt(i); if (Char > 127) { checkASCII_for_s2 = false; break; } } System.out.println( "is s1 contains only ASCII characters :" + checkASCII_for_s1); System.out.println( "is s2 contains only ASCII characters :" + checkASCII_for_s2); } } Outputis s1 contains only ASCII characters :true is s2 contains only ASCII characters :false Comment More infoAdvertise with us Next Article How to Check if a String contains only ASCII in Java? D devanshuma3v23 Follow Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read How to Check if a String Contains Only Lowercase Letters in Java? A string is a data structure that contains a set of characters. It can be a word or can be a sentence. Also, it can be empty or can have a single letter. A string can contain Uppercase letters, Lowercase letters, or numbers of special characters. In this article, we will learn how to check if a Stri 4 min read Check if a String Contains Only Alphabets in Java using ASCII Values Given a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse th 4 min read Check if a given string is Pangram in Java Given string str, the task is to write Java Program check whether the given string is a pangram or not. A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets. Examples: Input: str = "Abcdefghijklmnopqrstuvwxyz"Output: YesExplanation: The gi 3 min read How to Convert a String to Specific Character Encoding in Java? In Java, the process of converting a String to a certain character encoding is converting the string's character sequence to a byte array and applying the chosen character encoding. Java Program to Convert a String to a Specific Character EncodingUsing the String class's getBytes() function to trans 2 min read Like