繁体   English   中英

如何删除java中的最后一个字符串

[英]How to remove last character string in java

我想删除最后数据中的逗号。

例:

我有代码:

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"}

只需使用新的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);

}

你可以避免在第一时间附加它:

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()); 

}

或者在你的for循环之后添加它

    temp.setLength(temp.length() - 1);

这不需要在代码中进行常量的索引检查

您可以使用deleteCharAt()方法。

 StringBuilder s = new StringBuilder("{id: \"1\", name: \"Jhames\"},{id: \"2\", name: \"Richard\"},");
 System.out.println(s.deleteCharAt(s.lastIndexOf(",")));

首先,您不需要两个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())

这是非常无效的,因为:

  1. 每次执行此操作时都要创建单独的StringBuilder
  2. 这个新的StringBuilder需要不必要的调用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());

没有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("\"}");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM