Basically, I have some strings like
5 - Avenue Rd
9 - Bellamy
21 - Brimley
191 - Highway 27 Rocket
And so on. I'm looking for a way using Google Guava
to return the beginning digits. From what I've researched:
CharMatcher.DIGIT.retainFrom
is nice but I want it to stop at the first non-digit character. CharMatcher.DIGIT.trimLeadingFrom
is pretty much what I want, except that it returns the other part of the string where I want a string of digits instead. Any ideas?
EDIT: added another example.
Expected output for the four examples above:
5
9
21
191
Not quite equivalent, but
CharMatcher.DIGIT.negate().trimTrailingFrom(string);
Or -- a bit more involved, but this will work with intermediate digits --
string.substring(0, CharMatcher.DIGIT.negate().indexIn(string));
Or, with regexes, albeit with all the overhead that implies --
string.replaceAll("^(\\d+).*$", "$1")
Why don't you just split()
the string on the "-" sign and then trim()
and parse...
String test ="5 - Avenue Rd";
String[] out = test.split("-");
System.out.println(out[0].trim());
We don't need a framework to do that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.