您好,登錄后才能下訂單哦!
這篇文章運(yùn)用了實(shí)例代碼展示java用正則表達(dá)式判斷字符串是否為數(shù)字的方法,代碼非常詳細(xì),可供感興趣的小伙伴們參考借鑒,希望對(duì)大家有所幫助。
package com.yinxin.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { /** * 判斷一個(gè)字符串是否是數(shù)字。 * * @param string * @return */ public static boolean isNumber(String string) { if (string == null) return false; Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$"); return pattern.matcher(string).matches(); } private static void isNumberTest() { System.out.println(isNumber("580")); System.out.println(isNumber("5234254125424584")); System.out.println(isNumber("dfg15s4df5sd1fds")); } public static void main(String[] args) { isNumberTest(); } }
matches() 方法用于檢測(cè)字符串是否匹配給定的正則表達(dá)式。
調(diào)用此方法的 str.matches(regex) 形式與以下表達(dá)式產(chǎn)生的結(jié)果完全相同:
Pattern.matches(regex, str)
語(yǔ)法
public boolean matches(String regex)
參數(shù):regex -- 匹配字符串的正則表達(dá)式。
返回值:在字符串匹配給定的正則表達(dá)式時(shí),返回 true。
以上就是java用正則表達(dá)式判斷字符串是否為數(shù)字的方法,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。