![]() |
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
Output
is s1 contains only ASCII characters :true is s2 contains only ASCII characters :false Another ApproachWe 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:
Java
Output
is s1 contains only ASCII characters :true is s2 contains only ASCII characters :false |
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |