get Numbers From String by regular expression - Android java.util.regex

Android examples for java.util.regex:Number Pattern

Description

get Numbers From String by regular expression

Demo Code

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static Set<Integer> getNumsFromStr(String text) {
        text = null == text ? "" : text;
        String[] ary = text.replaceAll("[^\\d]", " ").split("\\s+");
        Set<Integer> set = new TreeSet<Integer>();
        for (String num : ary) {
            if (!num.trim().equals("")) {
                set.add(Integer.valueOf(num.trim()));
            }//from  w w  w . j  av a 2  s. c o m
        }
        return set;
    }

}

Related Tutorials