Java tutorial
//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())); } } return set; } }