我想删除最后数据中的逗号。
示例:
我有代码:
StringBuilder temp = new StringBuilder();
for (int i=0; i<allLines.size(); i++){
StringBuilder temp2 = new StringBuilder();
String[] abc = line.split("\t");
temp2.append("{");
temp2.append("id: \""+abc[0]+"\",");
temp2.append("name: \""+abc[1]+"\",");
temp2.append("},");
temp.append(temp2.toString());
}
System.out.println("result : "+temp.toString());
我有代码和结果:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"},
但我想要结果:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"}
答案 0 :(得分:8)
只需使用新的java 8 StringJoiner! (和其他漂亮的Java方法)
示例:
StringJoiner joiner = new StringJoiner(",");
joiner.add("foo");
joiner.add("bar");
joiner.add("baz");
String joined = joiner.toString(); // "foo,bar,baz"
它还支持Collectors.joining(",")
public static void main(String[] args) {
String input = "1\tJames\n2\tRichard";
String output = Arrays.stream(input.split("\n"))
.map( i -> String.format("{ id: \"%s\", name: \"%s\" }", i.split("\t")))
.collect(Collectors.joining(","));
//prints: { id: "1", name: "James" },{ id: "2", name: "Richard" }
System.out.println(output);
}
答案 1 :(得分:4)
您可以避免首先添加它:
for (int i=0; i<allLines.size(); i++){
StringBuilder temp2 = new StringBuilder();
String[] abc = line.split("\t");
temp2.append("{");
temp2.append("id: \""+abc[0]+"\",");
temp2.append("name: \""+abc[1]+"\",");
temp2.append("}");
if (i<allLines.size()-1)
temp2.append(",");
temp.append(temp2.toString());
}
答案 2 :(得分:3)
或者在for循环后添加此内容
temp.setLength(temp.length() - 1);
,不需要在代码中进行常量索引检查
答案 3 :(得分:2)
您可以使用deleteCharAt()方法。
StringBuilder s = new StringBuilder("{id: \"1\", name: \"Jhames\"},{id: \"2\", name: \"Richard\"},");
System.out.println(s.deleteCharAt(s.lastIndexOf(",")));
答案 4 :(得分:2)
首先,您不需要两个StringBuilder,而不是
StringBuilder sb1 = ..
for(..){
StringBuilder sb2 = ...
//fill sb2
sb1.append(sb2);
}
你应该使用
StringBuilder sb1 = ..
for(..){
//add all you want to sb1
sb1.append(..)
sb1.append(..)
}
接下来就是你不想做
sb.appent("foo" + x + "bar");
因为它与
相同sb.append(new StringBuilder("foo").append(x).append("bar").toString())
这是非常无效的,因为:
toString
方法,该方法必须将所有字符复制到新的String,稍后将其复制到构建器,而不是直接调用append(StringBuilder)
并复制其字符。所以代替sb.appent("foo" + x + "bar");
总是写
sb.appent("foo").append(x).append("bar");
现在让我们回到您的主要问题。由于您的代码没有声明line
变量,我假设是
String[] abc = line.split("\t");
你的意思是
String[] abc = allLines.get(i).split("\t");
所以你的代码看起来像
StringBuilder temp = new StringBuilder();
for (int i = 0; i < allLines.size(); i++) {
String[] abc = allLines.get(i).split("\t");
temp.append("{id: \"").append(abc[0]).append("\", ");
temp.append("name: \"").append(abc[1]).append("\"}");
if (i < allLines.size() - 1)
temp.append(", ");
}
System.out.println("result : " + temp.toString());
答案 5 :(得分:1)
没有Java 8解决方案:
StringBuilder temp = new StringBuilder();
adder(temp, allLines.get(0));
for (int i=1; i<allLines.size(); i++){
temp.append(",");
adder(temp, allLines.get(i));
}
System.out.println("result : "+temp.toString());
private static void adder(StringBuilder temp,String line){
String[] abc = line.split("\t");
temp.append("{id: \"");
temp.append(abc[0]);
temp.append("\",");
temp.append("name: \"");
temp.append(abc[1]);
temp.append("\"}");
}