導航:首頁 > 編程語言 > java子串判斷

java子串判斷

發布時間:2023-11-03 17:32:10

『壹』 java 怎麼判斷一個字元串中是否包含數字

java中判斷字元串是否為數字的方法:

1.用JAVA自帶的函數
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}

2.用正則表達式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}

3.使用org.apache.commons.lang
org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解釋:
isNumeric
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false

Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null

上面三種方式中,第二種方式比較靈活。

第一、三種方式只能校驗不含負號「-」的數字,即輸入一個負數-199,輸出結果將是false;

而第二方式則可以通過修改正則表達式實現校驗負數,將正則表達式修改為「^-?[0-9]+」即可,修改為「-?[0-9]+.?[0-9]+」即可匹配所有數字。

『貳』 java中怎麼判斷一個字元串中包含某個字元或字元串

Java中字元串中子串的查找共有四種方法,如下:

1、intindexOf(Stringstr):返回第一次出現的指定子字元串在此字元串中的索引。

2、intindexOf(Stringstr,intstartIndex):從指定的索引處開始,返回第一次出現的指定子字元串在此字元串中的索引。

3、intlastIndexOf(Stringstr)侍液團:返回在此字元串中最右邊出現的指定子字元串的索引。

4、intlastIndexOf(Stringstr,intstartIndex):從指定的索引處開始向後搜索,返回在此字埋唯符串中最後一次出現的指定子字元串的索引。

示例

下面的示例說明了 indexOf 方法的用法。

(str2){varstr1="BABEBIBOBUBABEBIBOBU"vars=str1.indexOf(str2);return(s);}publicclassFirstDemo{/***API中String的常用方法*///查找指定字元串是否存在publicstaticvoidmain(String[]args){Stringstr1="abcdefghijklmnabc";//從頭開始查找是否存在指定的字元System.out.println(str1.indexOf("c"));//從第四個字元位置開始往後繼續查找System.out.println(str1.indexOf("c",3));//若指定字元串中沒有該字元則系統返老橘回-1System.out.println(str1.indexOf("x"));

『叄』 JAVA語言 判斷字元串是否相等

java中判斷字元串是否相等有兩種方法:1、用「==」運算符,該運算符表示指向字元串的引用是否相同,比如: String a="abc";String b="abc",那麼a==b將返回true。這是因為在java中字元串的值是不可改變的,相同的字元串在內存中只會存一份,所以a和b指向的是同一個對象;再比如:String a=new String("abc"); String b=new String("abc");那麼a==b將返回false,因為a和b指向不同的對象。2、用equals方法,該方法比較的是字元串的內容是否相同,比如:String a=new String("abc"); String b=new String("abc"); a.equals(b);將返回true。所以通常情況下,為了避免出現上述問題,判斷字元串是否相等使用equals方法。

閱讀全文

與java子串判斷相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:611
php5314 瀏覽:350
完美國際安裝到哪個文件夾 瀏覽:663
什麼app可以掃一掃做題 瀏覽:534
程序員編碼論壇 瀏覽:918
淘點是什麼app 瀏覽:654
中國高等植物pdf 瀏覽:447
51單片機時間 瀏覽:176
後台如何獲取伺服器ip 瀏覽:260
單片機流水燈程序c語言 瀏覽:227
程序員第二職業掙錢 瀏覽:234
運行里怎麼輸入伺服器路徑 瀏覽:833
pythonstepwise 瀏覽:502
劉一男詞彙速記指南pdf 瀏覽:56
php認證級別 瀏覽:361
方舟編譯啥時候推送 瀏覽:1003
php手機驗證碼生成 瀏覽:668
哲學思維pdf 瀏覽:9
凌達壓縮機有限公司招聘 瀏覽:527
weblogic命令部署 瀏覽:30