[英]Does googles guava have a java tryparse integer method or something similar?
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. 我有java guava库,并想知道他们是否有一个tryparse类型的helper方法,它会尝试将字符串解析为整数,如果失败则返回一个布尔值。
In c#, I can do: 在c#中,我可以这样做:
if(int.tryparse("somestring", out myInt)) {
}
Was hoping java has something similiar (don't want to re-invent the wheel if it exists). 希望java有类似的东西(如果它存在,不想重新发明轮子)。
Guava has Ints.tryParse(String) as of 11.0. 截至11.0,Guava有Ints.tryParse(String) 。 It doesn't work quite like the C# method (no out parameters) though. 它不像C#方法(没有输出参数)那样工作。 It returns an Integer
that's null
if the string couldn't be parsed, so for your example you'd use it like this: 如果无法解析字符串,它会返回一个null
的Integer
,因此对于您的示例,您可以像这样使用它:
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. 它似乎被设置为添加到版本11.状态设置为固定,我认为这意味着它在当前版本中或将在下一个版本中。
http://code.google.com/p/guava-libraries/issues/detail?id=660 http://code.google.com/p/guava-libraries/issues/detail?id=660
EDIT: It is in the API for 11: tryParse 编辑:它在11的API: tryParse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.