Remove extra delimiter at end of StringBuilder in Java
This post will discuss how to remove an extra delimiter at the end of StringBuilder class in Java.
An extra delimiter gets added while looping through a collection and appending each element to a StringBuilder object separated by a delimiter. Consider the following code that will add an extra delimiter at the end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringBuilder sb = new StringBuilder(); for (String color: colors) { sb.append(color).append(","); } System.out.println(sb); // prints BLUE,RED,GREEN, } } |
This is a very common problem which every Java developer must have faced at least once in his lifetime. Let’s discuss a few methods to address this problem:
1. Using for loop
This method still loops through the collection but does that in a clever way. Instead of appending the delimiter, it appends the delimiter at the starting of each value and uses the empty string as a delimiter for the first value of the collection. This way, no extra delimiter is added at the end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringBuilder sb = new StringBuilder(); String delimiter = ""; for (String color: colors) { sb.append(delimiter); delimiter = ","; sb.append(color); } System.out.println(sb); // BLUE,RED,GREEN } } |
2. Using a variable
Another option is to use a variable to keep track of the collection size and appends the delimiter only if we are not at the last element in the list. This way, we avoid adding an extra delimiter at the end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringBuilder sb = new StringBuilder(); int n = colors.size(); for (String color: colors) { sb.append(color); if (--n > 0) { sb.append(","); } } System.out.println(sb); // BLUE,RED,GREEN } } |
3. Using StringJoiner Class
In Java 8 and above, we can use StringJoiner class in place of StringBuilder, which takes a delimiter and construct a sequence of values separated by the delimiter. This class automatically handles the last delimiter and does not add the extra delimiter at the end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.StringJoiner; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringJoiner sj = new StringJoiner(","); for (String color: colors) { sj.add(color); } System.out.println(sj); // BLUE,RED,GREEN } } |
4. Using setLength() or deleteCharAt() methods
The following solutions allows the loop to add the extra character at the end of StringBuilder, but removes it using utility methods provided by StringBuilder class:
⮚ Using setLength() method
The setLength() method allows us to set the length of a StringBuilder object. We can set the length of the StringBuilder to one less than its current length, which will effectively remove the last character.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringBuilder sb = new StringBuilder(); for (String color: colors) { sb.append(color).append(","); } if (sb.length() > 0) { sb.setLength(sb.length() - 1); } System.out.println(sb); // BLUE,RED,GREEN } } |
⮚ Using deleteCharAt() method
The deleteCharAt() method allows us to remove a character from a StringBuilder object by specifying its index. We can use sb.length() - 1 as the index to remove the last character.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLUE", "RED", "GREEN"); StringBuilder sb = new StringBuilder(); for (String color: colors) { sb.append(color).append(","); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } System.out.println(sb); // BLUE,RED,GREEN } } |
That’s all about removing the extra delimiter at the end of StringBuilder in Java.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)