Java StringUtils.isNumeric() 方法规范在逻辑上是否正确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3977840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:50:52  来源:igfitidea点击:

Is StringUtils.isNumeric() method specification logically correct?

javastringapache-commons-lang

提问by Andriy Sholokh

Apache's StringUtils.isNumeric()method specification says:
Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false. Nullwill return false. An empty String ("")will return true.

Apache 的StringUtils.isNumeric()方法规范说:
检查字符串是否仅包含 unicode 数字。小数点不是 unicode 数字并返回false. Null会回来false。空String ("")将返回true

Is this logically right? Why do they see empty string as numeric?

这在逻辑上是对的吗?为什么他们将空字符串视为数字?

采纳答案by Andriy Sholokh

There was not only me who asked this question :) People were opening this defect in Apache's Jira: https://issues.apache.org/jira/browse/LANG-428

不仅仅是我问了这个问题:) 人们在 Apache 的 Jira 中打开了这个缺陷:https: //issues.apache.org/jira/browse/LANG-428

They closed without fixing it only to keep backwards compatibility (to follow method specification).

他们关闭而不修复它只是为了保持向后兼容性(遵循方法规范)。

But everybody agreed that current behavior of method is wrong.

但是大家一致认为方法的当前行为是错误的。

回答by aioobe

The behavior changed in 3.0. From What's new in Commons Lang 3.0?:

该行为在 3.0 中发生了变化。来自Commons Lang 3.0 的新内容?

StringUtils.isAlpha, isNumeric and isAlphanumeric now all return false when passed an empty String. Previously they returned true.

StringUtils.isAlpha、isNumeric 和 isAlphanumeric 现在在传递空字符串时都返回 false。以前他们返回true。

Keeping old answer below, for reference and for pre 3.0 users.

保留下面的旧答案,供参考和 3.0 之前的用户使用。



Is this logically right?

这在逻辑上是对的吗?

We have

我们有

  1. the behavior of the method
  2. the documentation of the method (which is often viewed as the specification or contract)
  3. the name of the method
  1. 方法的行为
  2. 方法的文档(通常被视为规范或合同)
  3. 方法的名称

In this case 1 and 2 agree with each other; All characters in the empty string are unicode digits. (Or equivalently, no characters in the empty string are notunicode digits.) This what logicians call vacuously trueand somewhat counter intuitive. It's like saying that all elephants in my apartment are green. It's true, since there are no elephants in my apartment.

在这种情况下,1 和 2 彼此一致;空字符串中的所有字符都是 unicode 数字。(或者等效地,空字符串中的任何字符都不是unicode 数字。)这就是逻辑学家所说的空洞的真实和有点违反直觉的东西。这就像说我公寓里的所有大象都是绿色的。这是真的,因为我的公寓里没有大象。

Item 3 however (the name of the method) is naturally interpreted as a method that returns true if the given string represents a number.

但是,第 3 项(方法的名称)自然会被解释为如果给定的字符串表示数字则返回 true 的方法。

So, either it's a documentation and implementation bug, or it's a naming bug. There's no right or wrong answer to that.

因此,要么是文档和实现错误,要么是命名错误。对此没有正确或错误的答案。

A bug was filed here. The maintainers take the stand point that it's intended behavior.

这里提交一个错误。维护者认为这是预期的行为。

Why do they see empty string as numeric?

为什么他们将空字符串视为数字?

While the name of the method may lead you to believe the method should return true only for strings that represents a number, the spec actually says it should return true for if the string contains only unicode digits.

虽然该方法的名称可能会让您相信该方法应该只对表示数字的字符串返回 true,但规范实际上说,如果字符串仅包含 unicode 数字,它应该返回 true。

You say,

你说,

I'm confused because specification says: "Checks if the String contains only unicode digits." I don't see that "" contains digits....

我很困惑,因为规范说:“检查字符串是否只包含 unicode 数字。” 我没有看到“”包含数字....

Note that the empty string does not contain anything else than unicode digits. Therefore the method returns true.

请注意,空字符串不包含除 unicode 数字以外的任何内容。因此该方法返回true。

回答by irreputable

java.lang.Integer.parseInt("")will fail.

java.lang.Integer.parseInt("")将失败。

It's not a matter of logic. It's not a matter of common sense either - there wasn't any number that's represented by no symbol. There's no strong argument why an empty string should represent 0.

这不是逻辑问题。这也不是常识问题 - 没有任何数字是没有符号表示的。没有强有力的论据为什么空字符串应该代表 0。

If the method name is containsOnlyNumeric(), it is natural to return true for "" according to our math textbooks. However, the method name is isNumeric(), the treatment of "" isn't natural. Also, there's no apparent reason why nullshould return false. I would throw exception for null.

如果方法名是containsOnlyNumeric(),根据我们的数学教科书,很自然地为“”返回true。但是,方法名称是isNumeric(),“”的处理并不自然。此外,没有明显的理由为什么null应该返回 false。我会为空抛出异常。

But it is what it is, it is well documented and what more can you ask for?

但事实就是如此,它有据可查,您还能要求什么?

回答by Ranga Reddy

first check the condition the string is empty() or not.

首先检查字符串是否为空()的条件。

if(StringUtils.isNotEmpty(str) && StringUtils.isNumeric(str)) {

}

then your problem will be solved.

那么你的问题就解决了。

but still one more problem is you pass negative values like

但还有一个问题是你传递了负值,比如

str = "-1";

StringUtils.isNumeric(str)it will be false.

StringUtils.isNumeric(str)它会是假的。

You need to take care for this condition.

您需要注意这种情况。

回答by Najor

There is another solution. NumberUtils.isNumberThis checks if it is a number either Long, Double, Integer.

还有另一种解决方案。NumberUtils.isNumber这将检查它是否是 Long、Double 或 Integer 的数字。

Hope this help

希望这有帮助

回答by squaregoldfish

As of Commons Lang 3.5, NumberUtils.isCreatableis the way to do this. See this answerfor a more detailed description.

从 Commons Lang 3.5 开始,NumberUtils.isCreatable是实现此目的的方法。有关更详细的说明,请参阅此答案