Java intercepts numbers in a string

Source: Internet
Author: User

Java extracts numbers from a string

Give you a string that contains numbers, such as:

String s= "EERT343DFG56756DTRY66FGGG89DFGF";

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

1 methods provided by the String class:

Package test exercise; Import java.util.*; public class Get_stringnum {
/** *2012.6.2 */public static void main (string[] args) {String str = "Love23next234csdn3423javaeye"; Str=str.trim (); S Tring 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, can only extract the numbers together, can not be extracted separately. Of course it can be improved, interested friends can try.

2 Regular Expressions

Import java.util.*; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Get_stringnum {
/** *2012.6.2 */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 two classes in the Java.util.regex software package, you can consult the API for specific usage. It is also not possible to extract numbers individually.

3 Collection Class Library

Import java.util.*; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Get_stringnum {
/** *2012.6.2 */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 {
/** *2012.6.2 */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]

Java intercepts numbers in a string

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.