Remove Last Comma from String in Java [5 ways]
1. Using substring() method (Java 1.7)
2. Using substring() method (Java 8)
3. Using replaceAll() method
4. Using StringBuilder/StringBuffer
5. Using StringUtils class chop() method
Read Also:
Remove last comma from String in Java
1. Using the substring() method (Java 1.7)
We can easily use the substring() method to remove last comma from String in Java as shown below in the example:
public class RemoveLastCommaFromString { public static void main(String args[]) { String givenString = "Alive, is, Awesome,"; System.out.println("Original string is: " + givenString); givenString = givenString.substring(0, givenString.length()-1); System.out.println("String after removing last comma: "+ givenString); } }
Original string is: Alive, is, Awesome,
String after removing last comma: Alive, is, Awesome
2. Using substring() method (Java 8)
Using Java8 also we can remove last comma from String as shown below in the example:
import java.util.ArrayList; import java.util.List; public class RemoveLastCommaFromString2 { public static void main(String args[]) { List<String> givenString = new ArrayList<>(); givenString.add("Alive, is, Awesome,"); givenString.add("Be, in, Present,"); givenString.add("Love, Yourself,"); System.out.println("Original strings are:"); givenString.forEach(str -> System.out.println(str)); System.out.println(""); System.out.println("List of String after removing last comma are: "); givenString.stream() .map( s -> s.substring(0, s.length() - 1)) .forEach( s -> System.out.println(s)); } }
Original strings are:
Alive, is, Awesome,
Be, in, Present,
Love, Yourself,
List of String after removing last comma are:
Alive, is, Awesome
Be, in, Present
Love, Yourself
3. Using replaceAll() method
We can also use the replaceAll() method to remove last comma from a string in Java as shown below in the example:public class RemoveLastCommaFromString3 { public static void main(String args[]) { String str = "Alive, is, Awesome,"; str = str.replaceAll(",$", ""); System.out.println(str); } }
Output: Alive, is, Awesome
4. Using StringBuilder/StringBuffer
Another method to remove last comma from String in Java is using StringBuffer's deleteCharAt() method as shown below in the example:
public class RemoveLastCommaFromString4 { public static void main(String args[]) { String str = "Be, in, Present,"; StringBuilder sb = new StringBuilder(str); sb.deleteCharAt(sb.length()-1); str = sb.toString(); System.out.println(str); } }
Output: Be, in, Present
5. Using the StringUtils class chop() method
We need to add the below dependency to get the Apache commons lang jar:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
After that, use StringUtils class's chop() method to remove last comma from String in Java as shown below in the example:
import org.apache.commons.lang3.StringUtils; public class RemoveLastCommaFromString5 { public static void main(String args[]) { String str = "Alive, is, Awesome,"; str = StringUtils.chop(str); System.out.println(str); } }
Output: Alive, is, Awesome
That's all for today. Please mention in the comments if you know any other way of removing the last comma from String in Java.