StringBuilder removes the comma at the end of the last record

I use StringBuilder to display a comma between records, but also get a comma at the end of the last record

How to manage this? I don't like to see a comma at the end of the last entry - because it's useless

for (int i = 0; i < arrayList.size(); i++) { String name = arrayList.get(i).getName(); int price = arrayList.get(i).getPrice(); stringBuilder.append(strName); stringBuilder.append(" - "+price+","); } stringFinal = stringBuilder.toString(); 
+4
source share
7 answers

Add a check for the last item.

 if (i < arrayList.size() - 1) { stringBuilder.append(" - "+price+","); } else { stringBuilder.append(" - "+price); } 

or

 stringBuilder.append(" - "+price); if (i < arrayList.size() - 1) { stringBuilder.append(","); } 
+3
source

Java 8 solution

You don't need validation when using Java 8. All you need to do is use StringJoiner (or String.join() ).

 StringJoiner joiner = new StringJoiner(","); for (int i = 0; i < arrayList.size(); i++) { String name = arrayList.get(i).getName(); int price = arrayList.get(i).getPrice(); joiner.add(strName + " - " + price); } String joinedString = joiner.toString(); 

And you can make it even cleaner by using the Stream API (thanks to @Sasha):

 String joinedString = String.join(",", list.stream().map( e -> e.getName() + " - " + e.getPrice()).collect(Collectors.toList())); 
+5
source

Or loop up to size - 1, and then add the last element after the operands without a comma.

 int i = 0, price = 0; String name = null; for (i = 0; i < arrayList.size() - 1; i++) { name = arrayList.get(i).getName(); price = arrayList.get(i).getPrice(); stringBuilder.append(strName); stringBuilder.append(" - "+price+","); } name = arrayList.get(i).getName(); price = arrayList.get(i).getPrice(); stringBuilder.append(strName); stringBuilder.append(" - "+price); stringFinal = stringBuilder.toString(); 
0
source
  for (int i = 0; i < arrayList.size(); i++) { String name = arrayList.get(i).getName(); int price = arrayList.get(i).getPrice(); stringBuilder.append(strName); if(i== arrayList.size()-1) stringBuilder.append(" - "+price); else stringBuilder.append(" - "+price+","); } stringFinal = stringBuilder.toString(); 
0
source

Instead of putting it last, you can write it in front: -

  String name = arrayList.get(0).getName(); int price = arrayList.get(0).getPrice(); stringBuilder.append(strName).append(" - "+price); for (int i = 1; i < arrayList.size(); i++) { name = arrayList.get(i).getName(); price = arrayList.get(i).getPrice(); stringBuilder.append(","+strName).append(" - "+price); } stringFinal = stringBuilder.toString(); 
0
source

You can simply delete the last character:

  stringBuilder.deleteCharAt(stringBuilder.length()-1); stringFinal = stringBuilder.toString(); 

that would be the easiest solution.

0
source
 int index = stringBuilder.lastIndexOf(","); if (index != -1){ stringBuilder.replace(index, index+1, ""); } String stringFinal = stringBuilder.toString(); 
0
source

All Articles