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.

Download  Run Code

 
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:

 
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.

 
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.

 
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:

 
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.

 
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:

 
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:

That’s all about deleting extra separator from the end of String in Java.

 
Related Post:

Remove extra delimiter at end of StringBuilder in Java