代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package java_test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author: gznc_pcc * @date:2018年6月1日 10:50:38 * @version : * */ class Main { public static void main(String[] args) { String lineString = "[\"1\"]" ; String line = "[\"on\",\"1\",\"5\",\"8\",\"10\"]" ; lineString = line.replaceAll( "[\"\\[\\]]" , "" ); //用""替換" [ ] String[] word = lineString.split( "," ); //以,切割 System.out.println(lineString); for ( int i= 0 ;i<word.length;i++){ Pattern pattern = Pattern.compile( "[0-9]*" ); //正則,匹配數字 Matcher matcher = pattern.matcher(word[i]); if (matcher.matches()){ System.out.println( "1:可以轉換" ); System.out.println(Integer.parseInt(word[i])); } else { System.out.println( "2:不能轉換" ); System.out.println(word[i]); } } } } |
總結
以上所述是小編給大家介紹的Java用正則對字符串進行處理并判斷是否能轉為數字,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/vpqtxzmzezeqjj9977/article/details/80535787