Just give you a string containing numbers, such as:
String s="eert343dfg56756dtry66fggg89dfgf";
So how do we extract the numbers? There are roughly the following methods, regular expressions, collection classes, and methods provided by String classes.
1 Methods provided by String class:
package test exercise;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:232343423This method has an obvious disadvantage, it can only extract all the numbers together, but not separately. Of course, it can also be improved. Interested friends can try it.
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:232343423Pattern, Matcher is two classes in the java.util.regex software package. You can check the API for specific usage. Also, numbers cannot be extracted individually.
3 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(ss); } System.out.print(ss); }}output:[2, 3, 2, 3, 4, 3, 4, 2, 3]It is obvious that using regular expressions we can extract numbers separately.
There is also an answer to find by looking up the documents, as follows:
/** * Get the 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; }The two judgment methods that match using regular expressions are as follows;
// Determine whether all strings are numbers public boolean isDigit(String strNum) { return strNum.matches("[0-9]{1,}"); } // Determine whether all strings are numbers public boolean isDigit(String strNum) { Pattern pattern = Pattern.compile("[0-9]{1,}"); Matcher matcher = pattern.matcher((CharSequence) strNum); return matcher.matches(); } // Intercept the number 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-number public String splitNotNumber(String content) { Pattern pattern = Pattern.compile("//D+"); Matcher matcher = pattern.matcher(content); while (matcher.find()) { return matcher.group(0); } return ""; }The above is the entire content of the simple example of java extracting numbers from strings brought to you by the editor. I hope everyone can support Wulin.com more~