0

如何对在Java中保存为字符串的变量X和否定变量X'字母进行排序?

Example: String string = "B*A'*D*H'"; result must
         String sortedString="A'*B*D*H'";
4

1 回答 1

4

变量是否总是用 分隔*?在那种情况下,它会很简单:

    String in = "B*A'*D*H'";
    String[] vars = in.split("\\*");

    Arrays.sort(vars);

    // Java is missing a simple php-like join function ;-(
    StringBuilder sorted = new StringBuilder();
    for(String s: vars){
        sorted.append(s);
        sorted.append("*");
    }
    sorted.deleteCharAt(sorted.length()-1);

你完成了;-)

于 2012-07-28T08:49:30.243 回答