Delete extra separator from the end of String in Java
This post will discuss how to delete an extra separator from the end of a string in Java.
When a collection is looped over and each item is appended to a string with a separator, the string will have an extra separator at the end. The following code shows an example of this problem.
|
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; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("BLACK", "GREY", "WHITE"); String separator = ","; StringBuilder str = new StringBuilder(); for (String color: colors) { str.append(color).append(separator); } System.out.println(str); // BLACK,GREY,WHITE, } } |
The output of the code is "BLACK,GREY,WHITE,", which has an unwanted comma at the end. This can be fixed in different ways, as explained below:
1. Using String class
One way to delete an extra separator from the end of a string in Java is to use some of the built-in methods of the String class. For example, we can use the following methods:
1. Using substring() method
The simplest solution is to use the substring() method which returns a new string that is a substring of the original string. The idea is to take a substring from the beginning and consider all the characters in the string except the last character. For example:
|
1 2 3 4 5 6 7 |
public static String removeExtraSeparator(String str) { if (str == null || str.isEmpty()) { return str; } return str.substring(0, str.length() - 1); } |
This method works if we know the exact length of the separator we want to delete. However, it may not work if we have multiple separators at the end or if we don’t know the length of the separator.
2. Using replaceFirst() or replaceAll() method
Another option is to define a regular expression that matches our separator at the end of the string. We can use the replaceFirst() or replaceAll() method to replace the first occurrences or all occurrences of a regular expression by another string. For example, the following solution uses the regex [,;/.]+$, which matches with one or more separators at the end of the string. Here, $ matches the position just after the string’s last character.
|
1 2 3 4 5 6 7 |
public static String removeExtraSeparator(String str) { if (str == null || str.isEmpty()) { return str; } return str.replaceFirst("[,;/.]+$", ""); // or use replaceAll() } |
This is really useful if we want to delete more than one occurrence of our separator, or there are more than one separator. However, it may not work if we have an unknown separator present at the end of the string.
2. Using Apache Commons Lang
The StringUtils class from Apache Commons Lang library provides several methods for deleting extra separators from the end of a string in Java. For example, we can use the following methods:
1. Using StringUtils.chop() method
The StringUtils.chop() method returns a new string that is a copy of the original string with one character removed from the end. It is, by far, the most readable solution. It also takes care of null or empty string.
|
1 2 3 |
public static String removeExtraSeparator(String str) { return StringUtils.chop(str); } |
This method useful if we want to delete only one occurrence of the separator from end of the string. If there are more than one separator, we can call it repeatedly until our string does not end with our separator.
2. Using StringUtils.removeEnd() method
The removeEnd() method removes the specified substring if it is at the end of the original string. We can use it to delete an extra separator from the end of a string by passing our separator as the suffix. For example:
|
1 2 3 |
public static String removeExtraSeparator(String str, String sep) { return StringUtils.removeEnd(str, sep); } |
This method works if we know the separator we want to delete. It will not work if we have multiple separators at the end or if we don’t know the value of the separator.
3. Using StringUtils.substring() method
The substring() method permits the negative indexes which are offset from the end of the string. We can use it as follows. Here -1 means count back from the end of the string by 1 character.
|
1 2 3 |
public static String removeExtraSeparator(String str) { return StringUtils.substring(str, 0, -1); } |
This method works if we have a single separator to delete. It will not work if we have multiple separators at the end.
4. Using StringUtils.stripEnd() method
The stripEnd() method removes the specified characters from end of the string. We can use this method to delete an extra separator from the end of a string by passing our separator as one of the characters. For example:
|
1 2 3 |
public static String removeExtraSeparator(String str) { return StringUtils.stripEnd(str, ",;/."); } |
This method works if we can specify all the possible characters that can be used as separators at the end of our string. However, it may not work if we have other characters that we want to keep or if we want to delete only one occurrence of our separator.
3. Using StringBuilder class
Another option is to convert the string to a StringBuilder instance, and use its deleteCharAt() method to delete the character present at the last index. Finally, convert StringBuilder back to a string. Here’s an example:
|
1 2 3 4 5 |
public static String removeExtraSeparator(String str) { return new StringBuilder(str) .deleteCharAt(str.length() - 1) .toString(); } |
That’s all about deleting extra separator from the end of String 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 :)