
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Is it possible to check if a String only contains ASCII in java?
Using regular expression
You can find whether a particular String value contains ASCII characters using the following regular expression −
\A\p{ASCII}*\z
The matches() method of the String class accepts a regular expression and verifies whether the current string matches the given expression if so, it returns true, else it returns false.
Therefore, Invoke the matches() method on the input/required string by passing the above specified regular expression as a parameter.
Example
import java.util.Scanner; public class OnlyASCII { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string value: "); String input = sc.nextLine(); //Verifying for ACCII boolean result = input.matches("\A\p{ASCII}*\z"); if(result) { System.out.println("String approved"); } else { System.out.println("Contains non-ASCII values"); } } }
Output1
Enter a string value: hello how are you String approved
Output2
Enter a string value: whÿ do we fall Contains non-ASCII values
Verifying each character
If you convert ASCII characters to integers all the results will be less than or equal to 127.
The charAt() method of the String class accepts an integer value and returns the character at specified index.
Using this method retrieve each character in the given String and verify whether they are greater than 127.
Example
import java.util.Scanner; public class OnlyASCII { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string value: "); String input =sc.next(); //Converting given string to character array char charArray[] = input.toCharArray(); boolean result = true; for(int i = 0; i < input.length(); i++) { int test = (int)input.charAt(i); if (test<=127) { result = true; }else if (test >127){ result = false; } } System.out.println(result); if(result) { System.out.println("String approved"); }else { System.out.println("Contains non-ASCII values"); } } }
Output1
Enter a string value: whÿ false Contains non-ASCII values
Output2
Enter a string value: hello true String approved
- Related Articles
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java using Regex
- Check if the String contains only unicode letters in Java
- Check if a string contains only alphabets in Java using Lambda expression
- Java Program to check if the String contains only certain characters
- How to check if a string contains only decimal characters?
- How to check if a Python string contains only digits?
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters or digits in Java
- How to check if a string contains only whitespace letters in Python?
- How to check if a string only contains certain characters in Python?
- Check if the String contains only unicode letters, digits or space in Java
- How to check if a string in Python is in ASCII?
- Java Program to validate if a String contains only numbers
- How to check if the string contains only digits in JavaScript?

Advertisements