正則表示式匹配字母並排序
阿新 • • 發佈:2019-02-07
//重寫Collections.sort方法
public static List<String> sort(List<String> textbooks) {
Collections.sort(textbooks, new Comparator<String>(){
public int compare(String s1, String s2) {
return s1.toUpperCase().compareTo(s2.toUpperCase());
}
});
return textbooks;
}
public static void main(String[] args) {
String s="zopab16c2d3ef45*-Z";
String arg="[a-zA-z]";
List list=new ArrayList<String>();
Pattern p =Pattern.compile(arg);
Matcher m=p.matcher(s);
while(m.find()) {
System.out.print(m.group());
list.add(m.group());
}
//區分大小寫排序
//Collections.sort(list);
//不區分大小寫排序
System.out.println(sort(list));
}
public static List<String> sort(List<String> textbooks) {
Collections.sort(textbooks, new Comparator<String>(){
public int compare(String s1, String s2) {
return s1.toUpperCase().compareTo(s2.toUpperCase());
}
});
return textbooks;
}
public static void main(String[] args) {
String s="zopab16c2d3ef45*-Z";
String arg="[a-zA-z]";
List list=new ArrayList<String>();
Pattern p =Pattern.compile(arg);
Matcher m=p.matcher(s);
while(m.find()) {
System.out.print(m.group());
list.add(m.group());
}
//區分大小寫排序
//Collections.sort(list);
//不區分大小寫排序
System.out.println(sort(list));
}