0

我有一个像这种格式的字符串

数字。主题、大学名称、位置 2

我想重新排序这样的格式

Number. Uni Name, Subject, Location 2

我可以通过这种方式进行操作

int index = Order .indexOf(",");
String newStr =Order .substring(index + 1, Order .length());

但是,我认为所有字符串都有不同的名称和长度,所以这可能行不通。所以,我的问题是如何以有效的方式动态地重新排序字符串格式?

问题已更新

4

7 回答 7

0

一种方法是正则表达式。这可能不是有效的方法,但它相当干净:

public static void main(String args[]) {
    final Pattern pattern = Pattern.compile("(\\d++)\\.([^,]++),\\s*+([^,]++),\\s*+(.*+)");
    final Matcher matcher = pattern.matcher("");
    //for each input string
    final String input = "Subject, Uni Name, Location 2";
    matcher.reset(input);
    final String output = matcher.replaceAll("$1, $3, $2, $4");
    System.out.println(output);
}

因此,您可以预编译并重Pattern用它以Matcher获得最大速度。然后,您只需replaceAll对每个 input执行 a String

于 2013-09-19T15:56:04.390 回答
0
    String[] str = Order.split(",");

    System.out.println(str[1] +","+str[0]+","+str[2]);
于 2013-09-19T15:49:08.857 回答
0

你需要使用String#split()函数。在这里你可以找到一个很好的例子
所以你可以像这样解决你的问题

String[] str = Order.split(",");
String newOrderedString = str[1] +","+str[0]+","+str[2];
//OR
newOrderedString = str[1] +","+str[2]+","+str[0];
//OR
newOrderedString = str[2] +","+str[0]+","+str[1]; // As you want
于 2013-09-19T15:49:14.263 回答
0

像这样的东西可以工作:

String s = "Subject, Uni Name, Location 2";
String[] sp = s.split(",");
String finalString = sp[1] + "," + sp[0] + "," + sp[2];

您可以考虑使用 StringBuilder 来进行 finalString 组合。

于 2013-09-19T15:49:46.293 回答
0

利用:

String s = "Subject, Uni Name, Location 2";
String[] strs = s.split(",");

然后按您需要的任何顺序加入结果(我使用 trim() 去除多余的空格并将它们添加回需要的地方):

String t = strs[1].trim() + ", " + strs[0].trim() + ", " + strs[2].trim();

如果您经常这样做(例如在循环中),请使用 StringBuilder 来构建生成的字符串 - 它更有效(并且惯用)。

于 2013-09-19T15:53:29.607 回答
0

尝试

 String original = "Subject, Uni Name, Location 2";
 String[] split = original.split(",");
 StringBuilder b= new StringBuilder();
 b.append(split[1]).append(",");
 b.append(split[0]).append(",");
 b.append(split[2]);
 System.out.println(b.toString());  // Uni Name,Subject, Location 2
于 2013-09-19T15:51:32.220 回答
0

我会说不要对这样的原始字符串执行此操作。使用一个对象。

class ReportHeader {
    String number;
    String name;
    String subject;
    String location;

    ReportHeader(number,name,subject,location) {
        this.number = number;
        this.name = name;
        this.subject = subject;
        this.location = location;
    }

}

class interface HeaderFormat {
    String toString(ReportHeader header);
}

// this is how Wilson likes his reports
class ProfessorWilsonsHeaderFormat implements HeaderFormat { 
    public String toString(ReportHeader header) {
        return header.number + "," + header.name + "," + header.subject + "," + header.location;
    }
}

// this is how Thomspon likes his reports
class ProfessorThompsonsHeaderFormat implements HeaderFormat { 
    public String toString(ReportHeader header) {
        return header.number + "," + header.subject + "," + header.name + "," + header.location;
    }
}
于 2013-09-19T16:50:50.510 回答