How to remove the last line of characters in java
I want to remove the comma in the last data.
Example:
I have a code:
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());
Code and result:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"},
but I want the result:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"}
source to share
Just use the new java 8 StringJoiner ! (And other great Java methods)
Example:
StringJoiner joiner = new StringJoiner(",");
joiner.add("foo");
joiner.add("bar");
joiner.add("baz");
String joined = joiner.toString(); // "foo,bar,baz"
It also supports streams in the form Collectors.joining(",")
Complete example:
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);
}
source to share
You can avoid adding it in the first place:
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());
}
source to share
No Java 8 solution:
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("\"}");
}
source to share
First of all you don't need two StringBuilders, so instead of
StringBuilder sb1 = ..
for(..){
StringBuilder sb2 = ...
//fill sb2
sb1.append(sb2);
}
you must use
StringBuilder sb1 = ..
for(..){
//add all you want to sb1
sb1.append(..)
sb1.append(..)
}
The next thing you never want to do
sb.appent("foo" + x + "bar");
since it is similar
sb.append(new StringBuilder("foo").append(x).append("bar").toString())
which is very inefficient because:
- you create a separate StringBuilder every time you do this
- this new StringBuilder needs an unnecessary method call
toString
, which is to copy all the characters into a new String, which will later be copied to the builder, instead of callingappend(StringBuilder)
and copy its characters directly.
So instead of sb.appent("foo" + x + "bar");
always write
sb.appent("foo").append(x).append("bar");
Now, back to your main problem. Since your code does not have a variable declaration line
, I will assume that
String[] abc = line.split("\t");
you mean
String[] abc = allLines.get(i).split("\t");
So your code might look like
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());
source to share