2014-02-21 51 views
7

我的想法是這樣的,但我不知道正確的代碼如何檢查一個字符串只包含數字和一個小數點?

if (mystring.matches("[0-9.]+")){ 
    //do something here 
}else{ 
    //do something here 
} 

我覺得我幾乎沒有。唯一的問題是多個小數點可以出現在字符串中。我確實在尋找這個答案,但我無法找到答案。

+0

不是Java有一些通用的「count」或「count if」類型的函數可以處理任何類型的序列嗎? – Kaz

+0

是否有你想使用正則表達式的原因?特別是你這樣做,'String.indexOf'會更快。 – Jared

+0

indexOf不會告訴你它是否是一個數字;-) – Leo

回答

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

如果你想 - >確保它是一個數字,只有一個小數 < - 試試這個正則表達式來代替:

if(mystring.matches("^[0-9]*\\.?[0-9]*$")) { 
    // Do something 
} 
else { 
    // Do something else 
} 

這個表達式規定:

  1. ^表示字符串必須以此開頭。
  2. 依次爲或更多數字(*爲此)。
  3. 可選有一個小數(?做這個)。
  4. 關注或更多數字(*爲此)。
  5. 而$意味着它必須以此結束。

請注意,例如,項目符號#2將捕捉輸入「.02」的人。

如果無效化妝的正則表達式:"^[0-9]+\\.?[0-9]*$"

  • 唯一的區別是一個+號。這將迫使小數前面有一個數字:0.02
+0

邊緣情況:對於非程序員寫入0 BRFennPocock

+0

@BRPocock謝謝,沒錯。更改爲* – Timeout

+0

假定「。」或''是有效的,但奇怪的零,這工作:-) ...也許相當'^([0-9] + \。[0-9] * | \。[0-9] +)$'是偏執狂 – BRFennPocock

2

我認爲使用正則表達式會使答案複雜化。一個簡單的方法是使用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 
} 
+0

從這我將如何添加一個「if」來檢查數字嗎? – user3320339

+1

非常有幫助的回答!..一個更正..如果(mystring.substring(index + 1).indexOf(...))獲得邏輯權限 – KurinchiMalar

+0

@KurinchiMalar進行了校正,但是,正如其他註釋所述,我不檢查數字字符串 –

1

最簡單的

例子:

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

Digits check沒有完成。「abc.de」在這種情況下也會起作用。 – LearningEveryday

1

如果你想檢查一個數字(正)有一個點,如果你想要使用正則表達式,您必須避開點,因爲點意味着「任何字符」:-)

請參閱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] 

,所以你可以使用類似

System.out.println(s.matches("[0-9]+\\.[0-9]+")); 

PS。這也會匹配01.1等數字。我只想說明\\。

2

你可以使用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 
} 
0

使用下面的正則表達式的解決您的proble

  1. 允許保留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. 允許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. 
    
0

我創建自己解決問題的準確的問題。 我將分享你們的正則表達式:

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

看看這個Online Regex看到正常工作

參考文檔,如果你想繼續定製正則表達式

Documentation

相關問題