如何检查一个字符串只包含数字和一个小数点出现?

时间:2021-07-28 01:41:52

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.

我想我快到了。唯一的问题是可以在字符串中显示多个小数点。我确实在寻找这个答案,但我找不到答案。

8 个解决方案

#1


9  

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:

这个正则表达式:

  1. The ^ means the string must start with this.
  2. ^意味着字符串必须从这开始。
  3. Followed by none or more digits (The * does this).
  4. 后面是一个或多个数字(*做这个)。
  5. Optionally have a single decimal (The ? does this).
  6. 可选地有一个小数(?这样做)。
  7. Follow by none or more digits (The * does this).
  8. 后面跟着一个或多个数字(*做这个)。
  9. And the $ means it must end with this.
  10. 美元意味着它必须以这个结束。

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]*$"

如果这不是有效的正则表达式:“^[0 - 9]+ \ \。?[0 - 9]* $”

  • Only difference is a + sign. This will force the decimal to be preceded with a digit: 0.02
  • 唯一的区别是+号。这将迫使小数点前面加上一个数字:0.02

#2


2  

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
}

#3


2  

I think using regexes complicates the answer. A simpler approach is to use indexOf() and substring():

我认为使用regexes会使答案复杂化。一种更简单的方法是使用indexOf()和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 
}

#4


1  

Simplest

简单的

Example:

例子:

"123.45".split(".").length();

#5


1  

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" :-)

如果您想检查一个数字(正数)是否有一个点,如果您想使用regex,您必须转义这个点,因为这个点表示“任意字符”:-)

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 \\.

ps.这也将匹配编号01.1。我只是想举例说明。

#6


0  

int count=0;
For(int i=0;i<mystring.length();i++){    
    if(mystring.charAt(i) == '/.') count++;
}
if(count!=1) return false;

#7


0  

Use the below RegEx its solve your proble

使用下面的RegEx解决你的问题。

  1. allow 2 decimal places ( e.g 0.00 to 9.99)

    允许有两个小数点(e。g 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.
    
  2. allow 1 decimal places ( e.g 0.0 to 9.9)

    允许有1个小数点(e。g 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.
    

#8


0  

I create myself to solve exactly question's problem. I'll share you guys the regex:

我创造自己去解决问题。我将和你们分享regex:

^(\d)*(\.)?([0-9]{1})?$

^(\ d)*(\)?美元([0 - 9]{ 1 })?

Take a look at this Online Regex to see work properly

看看这个在线Regex,看看它是否正常工作

Refer documentation if you wish continue to custom the regex

如果您希望继续自定义regex,请参考文档

Documentation

文档

#1


9  

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:

这个正则表达式:

  1. The ^ means the string must start with this.
  2. ^意味着字符串必须从这开始。
  3. Followed by none or more digits (The * does this).
  4. 后面是一个或多个数字(*做这个)。
  5. Optionally have a single decimal (The ? does this).
  6. 可选地有一个小数(?这样做)。
  7. Follow by none or more digits (The * does this).
  8. 后面跟着一个或多个数字(*做这个)。
  9. And the $ means it must end with this.
  10. 美元意味着它必须以这个结束。

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]*$"

如果这不是有效的正则表达式:“^[0 - 9]+ \ \。?[0 - 9]* $”

  • Only difference is a + sign. This will force the decimal to be preceded with a digit: 0.02
  • 唯一的区别是+号。这将迫使小数点前面加上一个数字:0.02

#2


2  

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
}

#3


2  

I think using regexes complicates the answer. A simpler approach is to use indexOf() and substring():

我认为使用regexes会使答案复杂化。一种更简单的方法是使用indexOf()和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 
}

#4


1  

Simplest

简单的

Example:

例子:

"123.45".split(".").length();

#5


1  

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" :-)

如果您想检查一个数字(正数)是否有一个点,如果您想使用regex,您必须转义这个点,因为这个点表示“任意字符”:-)

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 \\.

ps.这也将匹配编号01.1。我只是想举例说明。

#6


0  

int count=0;
For(int i=0;i<mystring.length();i++){    
    if(mystring.charAt(i) == '/.') count++;
}
if(count!=1) return false;

#7


0  

Use the below RegEx its solve your proble

使用下面的RegEx解决你的问题。

  1. allow 2 decimal places ( e.g 0.00 to 9.99)

    允许有两个小数点(e。g 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.
    
  2. allow 1 decimal places ( e.g 0.0 to 9.9)

    允许有1个小数点(e。g 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.
    

#8


0  

I create myself to solve exactly question's problem. I'll share you guys the regex:

我创造自己去解决问题。我将和你们分享regex:

^(\d)*(\.)?([0-9]{1})?$

^(\ d)*(\)?美元([0 - 9]{ 1 })?

Take a look at this Online Regex to see work properly

看看这个在线Regex,看看它是否正常工作

Refer documentation if you wish continue to custom the regex

如果您希望继续自定义regex,请参考文档

Documentation

文档