将字符串对象分隔成令牌?

按如下所示给定String对象时,应将这些值分成几部分,用“,ve对其进行排序,然后通过将数字分组来返回List。”

String=123456 4568 567852 78212.





plmoknnm 回答:将字符串对象分隔成令牌?

使用比较器,您可以尝试以下操作:

public static void main(String[] args) {
    String input = "11B,11A,12,12/1,13,14,15,16,16A,17,17/1,57-51B,57,57-51A,1,10,11,53B,53A,53C,57-51C,57A";
    List<String> items = Arrays.asList(input.split("\\s*,\\s*"));


    Collections.sort(items,new Comparator<String>() {
        public int compare(String o1,String o2) {

            String o1StringPart = o1.replaceAll("\\d","");
            String o2StringPart = o2.replaceAll("\\d","");


            if(o1StringPart.equalsIgnoreCase(o2StringPart))
            {
                return extractInt(o1) - extractInt(o2);
            }
            return o1.compareTo(o2);
        }

        int extractInt(String s) {
            String num = s.replaceAll("\\D","");
            // return 0 if no digits found
            return num.isEmpty() ? 0 : Integer.parseInt(num);
        }
    });

    for (String s : items) {
        System.out.println(s);
    }
}

就期望的输出而言,这不能完全满足您的要求,但可以帮助您入门。

本文链接:https://www.f2er.com/3119439.html

大家都在问