Java 如何检查一个字符串只包含数字和小数点出现一次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21923361/
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
How to check a string contains only digits and one occurrence of a decimal point?
提问by user3320339
My idea is something like this but I dont know the correct code
我的想法是这样的,但我不知道正确的代码
if (mystring.matches("[0-9.]+")){
//do something here
}else{
//do something here
}
I think I'm almost there. The only problem is multiple decimal points can be present in the string. I did look for this answer but I couldn't find how.
我想我快到了。唯一的问题是字符串中可以存在多个小数点。我确实在寻找这个答案,但我找不到方法。
回答by ltalhouarne
int count=0;
For(int i=0;i<mystring.length();i++){
if(mystring.charAt(i) == '/.') count++;
}
if(count!=1) return false;
回答by Timeout
If you want to -> make sure it's a number AND has only one decimal<- try this RegEx instead:
如果你想 ->确保它是一个数字并且只有一位小数<- 试试这个 RegEx:
if(mystring.matches("^[0-9]*\.?[0-9]*$")) {
// Do something
}
else {
// Do something else
}
This RegEx states:
此正则表达式指出:
- The ^ means the string must start with this.
- Followed by noneor more digits (The * does this).
- Optionally have a single decimal (The ? does this).
- Follow by noneor more digits (The * does this).
- And the $ means it must end with this.
- ^ 表示字符串必须以此开头。
- 后跟一个或多个数字(* 就是这样做的)。
- 可以选择有一个小数(The ? 这样做)。
- 后跟一个或多个数字(* 就是这样做的)。
- 而 $ 意味着它必须以此结尾。
Note that bullet point #2 is to catch someone entering ".02" for example.
请注意,第 2 点是捕捉某人输入“.02”,例如。
If that is not valid make the RegEx: "^[0-9]+\\.?[0-9]*$"
如果这无效,则使 RegEx: "^[0-9]+\\.?[0-9]*$"
- Only difference is a + sign. This will force the decimal to be preceded with a digit: 0.02
- 唯一的区别是+号。这将强制小数点前面有一个数字:0.02
回答by Code-Apprentice
I think using regexes complicates the answer. A simpler approach is to use indexOf()
and substring()
:
我认为使用正则表达式会使答案复杂化。一种更简单的方法是使用indexOf()
and substring()
:
int index = mystring.indexOf(".");
if(index != -1) {
// Contains a decimal point
if (mystring.substring(index + 1).indexOf(".") == -1) {
// Contains only one decimal points
} else {
// Contains more than one decimal point
}
}
else {
// Contains no decimal points
}
回答by Orel Eraki
Simplest
最简单
Example:
例子:
"123.45".split(".").length();
回答by Leo
If you want to check if a number (positive) has one dot and if you want to use regex, you must escape the dot, because the dot means "any char" :-)
如果你想检查一个数字(正数)是否有一个点,如果你想使用正则表达式,你必须转义点,因为点意味着“任何字符”:-)
see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
见http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
so you can use something like
所以你可以使用类似的东西
System.out.println(s.matches("[0-9]+\.[0-9]+"));
ps. this will match number such as 01.1 too. I just want to illustrate the \\.
附:这也将匹配诸如 01.1 之类的数字。我只是想说明 \\.
回答by Brian Onn
You could use indexOf()
and lastIndexOf()
:
你可以使用indexOf()
和lastIndexOf()
:
int first = str.indexOf(".");
if ( (first >= 0) && (first - str.lastIndexOf(".")) == 0) {
// only one decimal point
}
else {
// no decimal point or more than one decimal point
}
回答by Kailas
Use the below RegEx its solve your proble
使用下面的正则表达式解决您的问题
allow 2 decimal places ( e.g 0.00 to 9.99)
^[0-9]{1}[.]{1}[0-9]{2}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {2} number length is one.
allow 1 decimal places ( e.g 0.0 to 9.9)
^[0-9]{1}[.]{1}[0-9]{1}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {1} number length is one.
允许 2 个小数位(例如 0.00 到 9.99)
^[0-9]{1}[.]{1}[0-9]{2}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {2} number length is one.
允许 1 个小数位(例如 0.0 到 9.9)
^[0-9]{1}[.]{1}[0-9]{1}$ This RegEx states: 1. ^ means the string must start with this. 2. [0-9] accept 0 to 9 digit. 3. {1} number length is one. 4. [.] accept next character dot. 5. [0-9] accept 0 to 9 digit. 6. {1} number length is one.
回答by topcbl
I create myself to solve exactly question's problem. I'll share you guys the regex:
我创造自己来解决问题的问题。我将与你们分享正则表达式:
^(\d)*(\.)?([0-9]{1})?$
^(\d)*(\.)?([0-9]{1})?$
Take a look at this Online Regexto see work properly
看看这个Online Regex看看是否正常工作
Refer documentation if you wish continue to custom the regex
如果您希望继续自定义正则表达式,请参阅文档