如何基于Java中的两个符号对字符串进行转换/排序?

时间:2019-01-14 11:00:11

标签: java string sorting java-8

我有以下字符串:

String string = "bbb,aaa,ccc\n222,111,333\nyyy,xxx,zzz";

我正在尝试将其转换为:

String converted = "aaa,bbb,ccc\n111,222,333\nxxx,yyy,zzz";

以某种方式排序。到目前为止,这是我尝试过的:

String[] parts = string.split("\\n");
List<String[]> list = new ArrayList<>();
for(String part : parts) {
    list.add(part.split(","));
}

for(int i = 0, j = i + 1, k = j + 1; i < list.size(); i++) {
    String[] part = list.get(i);
    System.out.println(part[i]);
}

因此,我设法分别从每个“单元”中获取第一个元素。但是如何获得所有商品并订购它们,以便得到结果?

使用Java8可以更简单吗?

谢谢!

4 个答案:

答案 0 :(得分:3)

我想做到这一点的一种方法是:

String result = Arrays.stream(string.split("\\n"))
                          .map(s -> {
                              String[] tokens = s.split(",");
                              Arrays.sort(tokens);
                              return String.join(",", tokens);
                          })
                          .collect(Collectors.joining("\\n"));

System.out.println(result); // aaa,bbb,ccc\n111,222,333\nxxx,yyy,zzz

请注意,如果您的模式比\n,更复杂-最好将这些Pattern提取出来

答案 1 :(得分:0)

    String string = "bbb,aaa,ccc\n222,111,333\nyyy,xxx,zzz";
    String converted = Arrays.stream(string.split("\\n"))
            .map(s -> Arrays.stream(s.split(","))
                    .sorted()
                    .collect(Collectors.joining(",")))
            .collect(Collectors.joining("\\n"));

答案 2 :(得分:0)

您可以使用老式的方式 ,而无需使用Java 8

public static void main(String[] args) {
    String s = "bbb,aaa,ccc\n222,111,333\nyyy,xxx,zzz";
    System.out.println(sortPerLine(s));
}

public static String sortPerLine(String lineSeparatedString) {
    // first thing is to split the String by the line separator
    String[] lines = lineSeparatedString.split("\n");

    // create a StringBuilder that builds up the sorted String
    StringBuilder sb = new StringBuilder();

    // then for every resulting part
    for (int i = 0; i < lines.length; i++) {
        // split the part by comma and store it in a List<String>
        String[] l = lines[i].split(",");
        // sort the array
        Arrays.sort(l);
        // add the sorted values to the result String
        for (int j = 0; j < l.length; j++) {
            // append the value to the StringBuilder
            sb.append(l[j]);
            // append commas after every part but the last one
            if (j < l.length - 1) {
                sb.append(", ");
            }
        }
        // append the line separator for every part but the last
        if (i < lines.length - 1) {
            sb.append("\n");
        }
    }

    return sb.toString();
}

但是,在我看来,Java 8应该是首选,所以请遵循其他答案之一。

答案 3 :(得分:0)

Pattern类使您可以流式传输直接拆分的String

String string = "bbb,aaa,ccc\n222,111,333\nyyy,xxx,zzz";
Pattern commaPattern = Pattern.compile(",");
String sorted = Pattern.compile("\n").splitAsStream(string)
    .map(elem -> commaPattern.splitAsStream(elem).sorted().collect(Collectors.joining(",")))
    .collect(Collectors.joining("\n"));