get Numbers From String - Android java.lang

Android examples for java.lang:String Parse

Description

get Numbers From String

Demo Code


//package com.java2s;

import java.util.Set;
import java.util.TreeSet;

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 .  java  2  s  .  c om*/
        }
        return set;
    }
}

Related Tutorials