How to Check if a String contains only ASCII in Java ?6 Jan 2025 | 3 min read The American Standard Code for Information Interchange is ASCII in its whole form. It is a Character represented numerically. Java uses the Unicode system and supports a number of languages. For the benefit of conciseness, let us understand that it first converts the character to a unique number, which is then converted to machine-level code. The range of ASCII values is 0 to 127. At this point, we want to determine if a string exclusively contains ASCII characters. The String.matches() method can be used, and it returns a boolean value when a Regex (regular expression) has been passed in. Approach: Using String.matches()Checking whether two strings contain just ASCII characters is the method used by the Java program provided. To do this check, it uses the String.matches() method and the regular expression \\A\\p{ASCII}*\\z. \\A\\p{ASCII}*\\z is a regular expression that guarantees that only ASCII characters (\\p{ASCII}) are used throughout the string, from start (\\A) to finish (\\z). The two strings that the software specifies are called s1 and s2, where s1 only has ASCII characters and s2 has both ASCII and non-ASCII characters. Next, for every string, it prints the outcome of the ASCII check. Implementation:FileName: AsciiChecking.java Output: Is the String s1 contains only ASCII characters :true Is the String s2 contains only ASCII characters :true Approach: Using for LoopConsidering the ASCII value range is known to be between 0 and 127, a string containing non-ASCII characters is present if any Unicode characters fall outside of this range. Algorithm: Step 1: We first define a boolean variable and set true to be its default value. Step 2: Since we first considered the string to contain solely ASCII characters Step 3: Next, we go over each character one by one and determine its range. If it does not fall between 0 and 127, we set the value of the boolean variable to false. Step 4: In a console window, print the result. Implementation: FileName: AsciiValuePrediction.java Output: Is the String str1 contains only ASCII characters :true Is the String str2 contains only ASCII characters :true Complexity Analysis: The above program's time complexity is O(N1 + N2), where N1 and N2 are the lengths of str1 and str2, respectively, and the Space Complexity is O(1). Next TopicHow-to-set-the-tls-version-in-java |
The binary operator XOR (Exclusive OR) is a basic operation in computer programming, including Java. It is an arithmetic operator that performs a bitwise exclusive OR operation on two operands of the same data type and returns a new value based on the outcome. In this...
4 min read
In Java, reducing is a terminal operation that aggregates a stream into a type or a primitive type. Java 8 provides Stream API contains set of predefined reduction operations such as average(), sum(), min(), max(), and count(). These operations return a value by combining the elements...
5 min read
In Java, Future is an interface that belongs to java.util.concurrent package. It is used to represent the result of an asynchronous computation. The interface provides the methods to check if the computation is completed or not, to wait for its completion, and to retrieve the...
24 min read
The Evil number is another special positive whole number in Java that has an even number of 1's in its binary equivalent. Unlike Prime and Armstrong numbers, Evil number is not so popular and asked by the interviewers. The numbers which are not evil are called odious...
3 min read
With the advent of multithreading and asynchronous programming, Java provides a potent method for task execution in parallel called the ExecutorService. However, you could encounter an ExecutionException when managing many threads and addressing errors that arise during task execution. In this section, we will examine the...
5 min read
In Java, Gson is defined as a library which was developed by the Google for serialization and deserialization of JSON (JavaScript Object Notation). The main purpose of Gson in Java is converting objects of Java into formats of JSON and vice versa. Let's understand in detail about...
7 min read
Java is a versatile and widely used programming language known for its platform independence and object-oriented approach. One of the fundamental concepts in Java programming is the use of classes and objects. A crucial aspect of this is the concept of a "Driver Class". In this...
2 min read
The zebra puzzle is a complex puzzle that requires a lot of effort and mental exercise to solve the puzzle. Sometimes is also known as Einstein's Puzzle or Einstein's Riddle because it is invented by the famous German physicist Albert Einstein. The puzzle is widely used in...
30 min read
The Caesar Cipher is a monoalphabetic substitution cipher. It is one of the simplest and most used encryption techniques. In this technique, each letter of the given text is replaced by a letter of some fixed number of positions down the alphabet. For example, with a...
7 min read
The problem is well known optimization problem which uses dynamic programming to achieve maximum profit - the Rod Cutting problem. Given a rod of fixed length, we want to cut this rod to get as much as possible while each piece has a different price, depending...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India