I've been using Guava's CharMatcher for a long time and it's worked great for everything. Now I see that most of the methods in it are now deprecated in Guava 27.0.1
The documentation says "Deprecated. Many digits are supplementary characters; see the class documentation." However, after reading the class documentation, I am still confused. Most of the time when a method is deprecated, they tell you an alternative way to do things, however, this time, I feel like the documentation is basically saying "This class doesn't really work correctly, so don't use it".
What is the correct way, for example, to only retain the digits of a string? Before I could simply do:
String inputString = "abc123def456";
String outputString = CharMatcher.javaDigit().retainFrom(inputString);
Javadoc for the method states:
@deprecated Many digits are supplementary characters; see the class documentation.
That means that besides numbers from 0 to 9 -- which you normally use -- there can be other character matched.
In Guava there are two built-in methods for that: digit()
and javaDigit()
. The former matches "BMP digit according to Unicode" specs, the latter matches "BMP digit according to Character#isDigit()
". Each of them match weird characters like Devanagari or Fullwidth
digits (I won't even link them ;)), which rarely is what users want and can be misleading.
That's why the preferred way (by Guava authors) is to be explicit (it's stated later in javadoc):
If you only care to match ASCII digits, you can use
CharMatcher#inRange('0', '9')
.
In your case simply use:
String outputString = CharMatcher.inRange('0', '9').retainFrom(inputString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With