Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for Guava's Charmatcher methods such as javaDigit()?

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);
like image 316
satnam Avatar asked Feb 24 '19 15:02

satnam


1 Answers

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);
like image 168
Xaerxess Avatar answered Oct 17 '22 19:10

Xaerxess