java從字符串中提取數字


string類函數的補充說明:

trim()方法返回調用字符串對象的一個副本,但是所有起始和結尾的空格都被刪除了,例子如下:String s="    Hello World      ".trim();就是把"Hello World"放入s中。(注意使用時必須賦值)

 

1 String類提供的方法:

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
package  測試練習;
import  Java.util.*;
public  class  get_StringNum {
 
 
/**
  *2016.10.25
  */
 
public  static  void  main(String[] args) {
String str = "love23next234csdn3423javaeye" ;
str=str.trim();
String str2= "" ;
if (str != null  && ! "" .equals(str)){
for ( int  i= 0 ;i<str.length();i++){
if (str.charAt(i)>= 48  && str.charAt(i)<= 57 ){
str2+=str.charAt(i);
}
}
 
}
System.out.println(str2);
}
 
}
 
output:
 
232343423

這個方法有個明顯的缺點,只能把數字全部提取到一起,不能分別提取。當然也可以改進,有興趣的朋友可以試試。

2 正則表達式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import  java.util.*;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
public  class  get_StringNum {
 
 
/**
  *2016.10.25
  */
 
public  static  void  main(String[] args) {
String a= "love23next234csdn3423javaeye" ;
String regEx= "[^0-9]"
Pattern p = Pattern.compile(regEx); 
Matcher m = p.matcher(a); 
System.out.println( m.replaceAll( "" ).trim());
}
 
}
 
output:
 
232343423

Pattern ,Matcher是java.util.regex軟件包里的兩個類,具體用法大家可以查閱一下api。同樣也不能單個提取數字。

  • Pattern類的作用在於編譯正則表達式后創建一個匹配模式.
  • Matcher類使用Pattern實例提供的模式信息對正則表達式進行匹配
  • Pattern complie(String regex) 
    由於Pattern的構造函數是私有的,不可以直接創建,所以通過靜態方法compile(String regex)方法來創建,將給定的正則表達式編譯並賦予給Pattern類

  • String pattern() 返回正則表達式的字符串形式,其實就是返回Pattern.complile(String regex)的regex參數


String regex = "\\?|\\*"; Pattern pattern = Pattern.compile(regex); String patternStr = pattern.pattern();//返回\?\*

replaceAll() 方法使用給定的參數 replacement 替換字符串所有匹配給定的正則表達式的子字符串

3 集合類庫

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
import  java.util.*;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
public  class  get_StringNum {
 
 
/**
  *2016.10.25
  */
 
public  static  void  main(String[] args) {
   String a= "love23next234csdn3423javaeye" ;
List<String> digitList = new  ArrayList<String>();
Pattern p = Pattern.compile( "[^0-9]" );
Matcher m = p.matcher(a);
String result = m.replaceAll( "" );
for  ( int  i = 0 ; i < result.length(); i++) {
digitList.add(result.substring(i, i+ 1 ));
 
}
System.out.println(digitList);
 
}
 
}
 
output:
 
[ 2 , 3 , 2 , 3 , 4 , 3 , 4 , 2 , 3 ]

相同的思路:

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
import  java.util.*;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
public  class  get_StringNum {
 
 
/**
  *2016.10.25
  */
 
public  static  void  main(String[] args) {
         String a= "love23next234csdn3423javaeye" ;
     List<String> ss = new  ArrayList<String>();
     for (String sss:s.replaceAll( "[^0-9]" , "," ).split( "," )){
       if  (sss.length()> 0 )
         ss.add(sss);
     }
     System.out.print(ss);
 
 
}
 
}
 
output:
 
[ 2 , 3 , 2 , 3 , 4 , 3 , 4 , 2 , 3 ]

很明顯,利用正則表達式我們就可以分別提取數字了。

另外還有一個利用查閱文檔找出的答案,如下:

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
32
33
34
35
36
37
38
39
40
/**
  * 從字符串文本中獲得數字
 
*@param
  text
 
*@return
  
 
*/
  
 
publicstatic
  List<Long>
  getDigit(String text) {
 
List<Long>
  digitList = new
  ArrayList<Long>();
  
 
Pattern p=
  Pattern.compile( "(\\d+)" );
  
 
Matcher m=
  p.matcher(text);
 
while
  (m.find()) {
 
String find=
  m.group( 1 ).toString();
 
digitList.add(Long.valueOf(find));
 
} return
  digitList;
 
}

兩個用正則表達式匹配的判斷方法,如下;

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
// 判斷一個字符串是否都為數字
public  boolean  isDigit(String strNum) {
   return  strNum.matches( "[0-9]{1,}" );
}
  
// 判斷一個字符串是否都為數字
public  boolean  isDigit(String strNum) {
   Pattern pattern = Pattern.compile( "[0-9]{1,}" );
   Matcher matcher = pattern.matcher((CharSequence) strNum);
   return  matcher.matches();
}
  
   //截取數字
   public  String getNumbers(String content) {
     Pattern pattern = Pattern.compile( "\\d+" );
     Matcher matcher = pattern.matcher(content);
     while  (matcher.find()) {
       return  matcher.group( 0 );
     }
     return  "" ;
   }
  
// 截取非數字
public  String splitNotNumber(String content) {
   Pattern pattern = Pattern.compile( "\\D+" );
   Matcher matcher = pattern.matcher(content);
   while  (matcher.find()) {
     return  matcher.group( 0 );
   }
   return  "" ;
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM