Guava CharMatcher.matchesNoneOf() method in Java
In this post, we will show you how to use the CharMatcher.matchesNoneOf() method in Java, and explain why it is useful and how it works. We will also compare it with other ways of checking if a string does not contain certain characters in Java, and highlight some of its advantages and limitations.
1. Overview of CharMatcher.matchesNoneOf() method
The CharMatcher.matchesNoneOf() method returns a boolean value that indicates whether a given string does not contain any character that matches the CharMatcher. The method signature is:
|
1 |
public boolean matchesAllOf(CharSequence sequence) |
The sequence parameter is a CharSequence object that represents the character sequence to be checked. The method will return true if all the characters in the sequence match the condition, or false otherwise.
To use the CharMatcher.matchesNoneOf() method, you need to create a CharMatcher that matches the characters you want to check for. There are many ways to create a CharMatcher in Guava, such as using factory methods or custom logic. For example, to create a CharMatcher that matches any :
- whitespace character, you can use the factory method
whitespace(). - lowercase letter, you can use the factory method
javaLowerCase(). - vowel (a, e, i, o, u), you can use the factory method
anyOf(). - character in a range (such as
A-Zor0-9), you can use the factory methodinRange(). - character that satisfies a custom condition, you can use the factory method
forPredicate().
Once you have created a CharMatcher, you can use it with the matchesNoneOf() method to check if a string does not contain any matching character. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import com.google.common.base.CharMatcher; class Main { public static void main(String[] args) { // Check if a string does not contain any whitespace CharMatcher whitespace = CharMatcher.whitespace(); boolean result = whitespace.matchesNoneOf("hello"); System.out.println(result); // true // Check if a string does not contain any lowercase letter CharMatcher lowerCase = CharMatcher.javaLowerCase(); result = lowerCase.matchesNoneOf("HELLO"); System.out.println(result); // true // Check if a string does not contain any vowel CharMatcher vowel = CharMatcher.anyOf("aeiou"); result = vowel.matchesNoneOf("hll"); System.out.println(result); // true // Check if a string does not contain any uppercase letter CharMatcher upperCase = CharMatcher.inRange('A', 'Z'); result = upperCase.matchesNoneOf("hello"); System.out.println(result); // true // Check if a string does not contain any digit CharMatcher digit = CharMatcher.inRange('0', '9'); result = digit.matchesNoneOf("hello"); System.out.println(result); // true // Check if a string does not contain any odd digit CharMatcher oddDigit = CharMatcher .forPredicate(c -> Character.isDigit(c) && (c - '0') % 2 == 1); result = oddDigit.matchesNoneOf("2468"); System.out.println(result); // true } } |
2. Advantages and Limitations of using the CharMatcher.matchesNoneOf() method
Using the CharMatcher.matchesNoneOf() method has many advantages, such as:
- It is concise and expressive. You can create a
CharMatcherthat matches any character you want, and use it with thematchesNoneOf()method in one line of code, without having to write complex regular expressions or loops. - It is efficient and optimized. It avoids doing needless allocation and copying of strings, and it employs a smart algorithm to carry out fast bitwise operations on characters.
- It is consistent and reliable. It does not rely on the locale or the platform, and it correctly handles Unicode characters.
However, this method depends on Guava. You may have to include Guava as a dependency in your project if you are not using it already.
3. Comparison with other ways in Java
There are other ways of checking if a string does not contain certain characters in Java, such as using the String class methods, regular expressions, or loops. How do they compare with using the CharMatcher.matchesNoneOf() method? Here are some differences:
The String class methods, such as contains(), indexOf(), or matches(), can only check for one character or a fixed sequence of characters at a time. They cannot check for multiple characters or a variable sequence of characters. For example, you cannot use these methods to check if a string does not contain any vowel or any digit.
Regular expressions can check for multiple characters or a variable sequence of characters, but they can be complex and hard to read and write. They also require compiling and matching patterns, which can be inefficient and error-prone. For example, to check if a string does not contain any vowel, you need to write something like this:
|
1 2 3 4 5 6 7 8 |
import java.util.regex.Pattern; class Main { public static void main(String[] args) { boolean result = !Pattern.compile("[aeiou]").matcher("hmm").find(); System.out.println(result); // true } } |
Loops can check for multiple characters or a variable sequence of characters, but they require writing more code and logic, which can be tedious and verbose. They also require iterating over the characters of the string, which can be inefficient and slow. For example, to check if a string does not contain any vowel, you need to write something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Main { public static void main(String[] args) { boolean result = true; for (char c : "hmm".toCharArray()) { if ("aeiou".indexOf(c) != -1) { result = false; break; } } System.out.println(result); // true } } |
4. Conclusion
In this post, we have shown you how to use Guava’s CharMatcher.matchesNoneOf() method in Java, and explained why it is useful and how it works. We have also compared it with other ways of checking if a string does not contain certain characters in Java, and highlighted some of its advantages and limitations.
If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)