Reputation: 91
I want to remove comma in last data.
example:
i have 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());
ihave code and result:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"},
but i want result:
{id: "1", name: "Jhames"},{id: "2", name: "Richard"}
Upvotes: 3
Views: 2331
Reputation: 5048
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("\"}");
}
Upvotes: 1
Reputation: 124225
First of all you don't need two StringBuilders, so instead of
StringBuilder sb1 = ..
for(..){
StringBuilder sb2 = ...
//fill sb2
sb1.append(sb2);
}
you should use
StringBuilder sb1 = ..
for(..){
//add all you want to sb1
sb1.append(..)
sb1.append(..)
}
Next thing is that you don't ever want to do
sb.appent("foo" + x + "bar");
because it is same as
sb.append(new StringBuilder("foo").append(x).append("bar").toString())
which is very ineffective because:
toString
method which has to copy all characters to new String which will later be copied to builder, instead of calling append(StringBuilder)
and copy its characters directly.So instead of sb.appent("foo" + x + "bar");
always write
sb.appent("foo").append(x).append("bar");
Now lets go back to your main problem. Since your code doesn't have declaration of line
variable I am assuming that by
String[] abc = line.split("\t");
you mean
String[] abc = allLines.get(i).split("\t");
So your code can 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());
Upvotes: 2
Reputation: 20029
Just use the new java 8 StringJoiner! (And other nifty 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 of 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);
}
Upvotes: 8
Reputation: 189
Alternatively add this after your for loop
temp.setLength(temp.length() - 1);
which requires no constant index checking in your code
Upvotes: 3
Reputation: 3881
You can use deleteCharAt() method.
StringBuilder s = new StringBuilder("{id: \"1\", name: \"Jhames\"},{id: \"2\", name: \"Richard\"},");
System.out.println(s.deleteCharAt(s.lastIndexOf(",")));
Upvotes: 2
Reputation: 393821
You can avoid appending 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());
}
Upvotes: 4