Java extracts a simple instance of a number from a string _java

Source: Internet
Author: User
Tags string find

Give you a string that contains numbers, such as:

String s= "EERT343DFG56756DTRY66FGGG89DFGF";

So how do we extract the numbers? There are roughly the following methods, regular expressions, collection classes, and the methods provided by the string class.

1 The method provided by the String class:

Package test practice;
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) <= {
Str2+=str.charat (i);
}
}} System.out.println (STR2);
}

Output:

232343423

This method has an obvious disadvantage, only the number can be extracted together, can not be extracted separately. Of course also can 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 is the Java.util.regex software package in the two classes, the specific use of the API you can check. Similarly, you cannot extract numbers 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]

The 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, we can extract the numbers separately using regular expressions.

There is also an answer that uses a lookup document to find the following:

/**
 * Obtains the number from the string literal * 

@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 methods for judging the matching of regular expressions are as follows;

Determines whether a string is a numeric public 
boolean isdigit (String strnum) {return 
  strnum.matches ("[0-9]{1,}"); 
} 
 
Determines whether a string is a digital public 
boolean isdigit (string strnum) {pattern Pattern 
  = Pattern.compile ("[0-9]{1,}"); 
  Matcher Matcher = Pattern.matcher ((charsequence) strnum); 
  return matcher.matches (); 
} 
 
  Intercepts the digital public 
  String getnumbers (string content) {pattern pattern 
    = pattern.compile ("\\d+"); 
    Matcher Matcher = pattern.matcher (content); 
    while (Matcher.find ()) {return 
      matcher.group (0); 
    } 
    Return ""; 
  } 
 
Intercepts 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 ""; 
} 

The above is a small series of Java from the string to extract the number of simple examples of all the content, I hope that many support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.