How to check a string contains only digits and one occurrence of a decimal point?

大城市里の小女人 提交于 2019-12-09 12:30:23

问题


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.


回答1:


If you want to -> make sure it's a number AND has only one decimal <- try this RegEx instead:

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. Followed by none or more digits (The * does this).
  3. Optionally have a single decimal (The ? does this).
  4. Follow by none or more digits (The * does this).
  5. And the $ means it must end with this.

Note that bullet point #2 is to catch someone entering ".02" for example.

If that is not valid make the RegEx: "^[0-9]+\\.?[0-9]*$"

  • Only difference is a + sign. This will force the decimal to be preceded with a digit: 0.02



回答2:


You could use indexOf() and 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:


I think using regexes complicates the answer. A simpler approach is to use 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 
}



回答4:


Simplest

Example:

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



回答5:


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

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




回答6:


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



回答7:


Use the below RegEx its solve your proble

  1. 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.
    
  2. 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.
    



回答8:


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

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

Take a look at this Online Regex to see work properly

Refer documentation if you wish continue to custom the regex

Documentation



来源:https://stackoverflow.com/questions/21923361/how-to-check-a-string-contains-only-digits-and-one-occurrence-of-a-decimal-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!