解析“true” /“假”爪哇语
正确解析布尔字符串的实用方法是什么?我的意思是
"true" => true
"false" => false
"foo" => error
java.lang.Boolean 中的解析方法很狡猾 - 它们无法区分“false”和“foo”。 Java 库(或 Guava 或 Commons Lang)中还有其他可以正确执行此操作的吗?
是的,这只是几行,我只是不想写任何我不应该写的行。 :-)
What's the utility method that parses a boolean String properly? By properly I mean
"true" => true
"false" => false
"foo" => error
The parse methods in java.lang.Boolean are dodgy - they don't distinguish "false" from "foo". Anything else in Java libraries (or Guava, or Commons Lang) that does it properly?
Yes it's just be a couple lines, I just rather not write any line that I shouldn't have to. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 apache commons 中的布尔实用程序:
布尔实用程序 API
Check out Boolean Utils form apache commons :
Boolean Utils API
没有一个。
There's not one.
老实说,这个问题很可笑。是的,有一些内置方法可以做到这一点(Apache Fan 提到的 Boolean utils API)。但是你会不遗余力地以奇特的方式做某事,但代价是 A) 生产力(别再浪费时间了,写三行代码)和 B) 可读性。什么更容易阅读:
或者
我每次都会选择第一个。更好的是,使用 IgnoreCase 选项进行字符串比较。 toBoolean 区分大小写,因此“True”实际上会引发异常。惊人的!这真的很有用!
Honestly, this question is ridiculous. Yes, there are ways to do it built in (the Boolean utils API Apache Fan mentioned). But you're going out of your way to do something in a fancy way at the cost of A) productivity (stop wasting your time, write the three lines of code), and B) readability. What's easier to read:
or
I'd go for the first one every time. Even better, use the IgnoreCase option for the string comparison. The toBoolean is case sensitive, so "True" would actually throw an exception. Awesome! That's really useful!
非常简单的怎么样:
编辑:或者我错过了重点......
How about the very simple:
EDIT: or am I missing the point...