How do you remove all the alphabetic characters from a string?

后端 未结 6 1573
时光说笑
时光说笑 2020-12-24 10:18

I have a string containg alphabetic characters, for example:

  1. 254.69 meters
  2. 26.56 cm
  3. 23.36 inches
6条回答
  •  甜味超标
    2020-12-24 11:06

    Use CharMatcher API from Google's Guava library:

    String magnitudeWithUnit = "254.69 meter";
    String magnitude = CharMatcher.inRange('a', 'z').or(inRange('A', 'Z')).removeFrom(magnitudeWithUnit);
    

    Do static-import CharMatcher.inRange(..). You can trim the results for trailing space.

提交回复
热议问题