java extracts a simple instance of a number from a string

  • 2020-05-12 02:38:45
  • OfStack

Give you a random string containing a number, such as:

String s="eert343dfg56756dtry66fggg89dfgf";

So how do we extract the Numbers? There are several general methods, regular expressions, collection classes, and methods provided by the String class.

Methods provided by class 1 String:


package  Practice tests ;
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

This method has an obvious disadvantage that it can only extract all the Numbers up to 1, and cannot extract them separately. Of course can also improve, interested friends can try.

2 regular expressions


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 are two classes in the java.util.regex software package. You can refer to api for details. Again, you can't extract a single number.

Collection class library


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]

Same idea:


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]

Obviously, using regular expressions we can extract the Numbers separately.

In addition, there is another answer that can be found by consulting the documents, as follows:


/**
 *  Get a number from the string text  

*@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; 

}

Two judgment methods matching with regular expressions are as follows.


//  judge 1 Are all strings Numbers  
public boolean isDigit(String strNum) { 
  return strNum.matches("[0-9]{1,}"); 
} 
 
//  judge 1 Are all strings Numbers  
public boolean isDigit(String strNum) { 
  Pattern pattern = Pattern.compile("[0-9]{1,}"); 
  Matcher matcher = pattern.matcher((CharSequence) strNum); 
  return matcher.matches(); 
} 
 
  // Interception of digital  
  public String getNumbers(String content) { 
    Pattern pattern = Pattern.compile("\\d+"); 
    Matcher matcher = pattern.matcher(content); 
    while (matcher.find()) { 
      return matcher.group(0); 
    } 
    return ""; 
  } 
 
//  Intercept non-digital  
public String splitNotNumber(String content) { 
  Pattern pattern = Pattern.compile("\\D+"); 
  Matcher matcher = pattern.matcher(content); 
  while (matcher.find()) { 
    return matcher.group(0); 
  } 
  return ""; 
} 

Related articles: