I have the java guava library, and was wondering if they have a tryparse type helper method that will attempt to parse a string to a integer, and return a boolean if it failed.
In c#, I can do:
if(int.tryparse("somestring", out myInt)) {
}
Was hoping java has something similiar (don't want to re-invent the wheel if it exists).
Guava has Ints.tryParse(String) as of 11.0. It doesn't work quite like the C# method (no out parameters) though. It returns an Integer
that's null
if the string couldn't be parsed, so for your example you'd use it like this:
Integer myInt = Ints.tryParse(someString);
if (myInt != null) {
...
}
It looks like this was set to be added to release 11. The status is set to fixed, which I would think means it's either in the current release or will be in the next.
http://code.google.com/p/guava-libraries/issues/detail?id=660
EDIT: It is in the API for 11: tryParse
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