簡體   English   中英

如何檢查TextField是否僅包含(DOT)字符

[英]How to check if the TextField contains only (DOT) character

如何檢查TextField是否僅包含(DOT)字符。 TextField可以包含以下內容:

.9 = > Valid
9. = > Valid
9.23 = >Valid

但是,如果TextField僅包含(。),則應該向用戶拋出錯誤消息。 我無法執行Text.startsWith()或都不執行Text.contains()因為在其他情況下兩者都可能失敗。

使用此正則表達式

text.matches("\\d+|\\d*\\.\\d+|\\d+\\.\\d*")
  • \\\\d+僅匹配數字
  • \\\\d*\\\\.\\\\d+匹配以點或數字開頭並包含點的字符串
  • \\\\d+\\\\.\\\\d*匹配以數字開頭,包含點和其后任意數量的數字的字符串

請記住,此表達式也將接受以0開頭的數字。

您可以簡單地使用

text.equals(".")

或者,考慮到空格,

text.trim().equals(".")

如果還必須檢查除點之外是否還有數字,則可能應該編寫幾個正則表達式。

在您的情況下,如果字符串符合以下模式,則該字符串有效:

\d+       // Digits without the dot
\d*\.\d+  // Number with some digits after the dot
\d+\.\d*  // Number with some digits before the dot

你可以只用兩種方法

if(yourTextFieldName.getText().endsWith(".") && yourTExtFieldName.startsWith(".")) {
    //write your code here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM