2

currently I need to find some special lexical characteristics of a string in Java such as

  • Total number of characters
  • Total number of alphabetic characters
  • Total number of uppercase characters
  • .... I wonder that is there a library can do that? It would be great to save coding time.
    Thank you very much.
Xitrum
  • 7,765
  • 26
  • 90
  • 126

3 Answers3

2

google guava can do that.

Have a look at the Strings module: http://code.google.com/p/guava-libraries/wiki/StringsExplained

Check out the CharMatcher on that site!

And after matching it, you just use the length() method of the remaining String.

Kenyakorn Ketsombut
  • 2,072
  • 2
  • 26
  • 43
0

There is no such library. You'll have to write the 20 lines of code to do it yourself:

int length = s.length(); // get the length
int alphaCount;
for( int i=0; i<length; i ++ ) {
    char c = s.charAt(i);
    alphaCount += toInt( Character.isAlpha( c ) );
    ...
}

with

int toInt( boolean b ) { return b ? 1 : 0; }
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

You can take a look at:

npinti
  • 51,780
  • 5
  • 72
  • 96
  • @Downvoter care to explain? It would be nice to have a force comments for downvotes... at least you know what others think you did wrong... – npinti Feb 25 '14 at 08:54