简体   繁体   English

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

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

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. 我确实找到了这个答案,但我找不到如何。

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: 此RegEx声明:

  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. 请注意,项目符号#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

I think using regexes complicates the answer. 我认为使用正则表达式会使答案复杂化。 A simpler approach is to use indexOf() and substring() : 一种更简单的方法是使用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 
}

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
}

Simplest 简单

Example: 例:

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

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. PS。 this will match number such as 01.1 too. 这也将匹配01.1等数字。 I just want to illustrate the \\\\. 我只想说明\\\\。

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

Use the below RegEx its solve your proble 使用以下RegEx解决您的问题

  1. allow 2 decimal places ( eg 0.00 to 9.99) 允许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. 
  2. allow 1 decimal places ( eg 0.0 to 9.9) 允许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. 

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 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM