To remove the ", " part which is immediately followed by end of string, you can do:

str = str.replaceAll(", $", "");

This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case.

Example code:

String str = "kushalhs, mayurvm, narendrabz, ";
str = str.replaceAll(", $", "");
System.out.println(str);  // prints "kushalhs, mayurvm, narendrabz"

NOTE: Since there has been some comments and suggested edits about the ", $" part: The expression should match the trailing part that you want to remove.

  • If your input looks like "a,b,c,", use ",$".
  • If your input looks like "a, b, c, ", use ", $".
  • If your input looks like "a , b , c , ", use " , $".

I think you get the point.

Answer from aioobe on Stack Overflow
🌐
Coderanch
coderanch.com › t › 400518 › java › remove-comma
best way to remove the last comma (Beginning Java forum at Coderanch)
But if you need to remove it, you can say: This will chomp the last character in the string. Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo. ... I'd also recommend using a StringBuffer/StringBuilder if you're going to be repeatedly concatenating a string... ... Look at String.endsWith() too. You might want to check to see if the comma is there before you remove it.
🌐
Quora
quora.com › What-is-the-simplest-way-to-remove-the-last-comma-of-a-String-in-Java
What is the simplest way to remove the last comma of a String in Java? - Quora
Answer (1 of 7): The simplest way to remove the last comma is to not add it in the first place. You can use a loop [code]String sep = ""; StringBuilder sb = new StringBuilder() ; for (Object x: list) { sb.append(sep).append(x); sep = ","; } [/code]Or you can use [code]String s = list....
🌐
CodeSpeedy
codespeedy.com › home › how to remove last comma from a string in java
How to remove last comma from a string in Java - CodeSpeedy
January 24, 2020 - Remove the last comma from a string in Java by using the replaceAll() method. ReplaceAll() method is easy and simple to use.
🌐
Benchresources
benchresources.net › home › java › java 8 – how to remove last comma (,) from string ?
Java 8 - How to remove last comma (,) from String ? - BenchResources.Net
September 19, 2022 - To remove last character i.e.; comma (,) pass 0 (zero) as start-index & (String length – 1) as end-index · package in.bench.resources.java8.string.methods; public class RemoveLastCommaInString { public static void main(String[] args) { // 1. test string with commas String str = "q,w,e,r,t,y,"; ...
🌐
Programming.Guide
programming.guide › java › remove-trailing-comma-from-comma-separated-string.html
Java: Removing trailing comma from comma separated string | Programming.Guide
In Java, difference between default, public, protected, and private ... Executing code in comments?! ... This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case. Note that this assumes that the string ends with , (comma followed by space).
🌐
Java2Blog
java2blog.com › home › core java › remove comma from string in java
Remove Comma from String in Java - Java2Blog
February 2, 2022 - We can use String’s substring() method to remove last comma from String in java.
🌐
Reactgo
reactgo.com › home › how to remove last comma (,) of a string in java
How to remove last comma (,) of a string in Java | Reactgo
July 17, 2023 - We remove the last comma of a string by using the built-in substring() method with first argument 0 and second argument string.length()-1 in Java. Slicing starts from index 0 and ends before last index that is string.length()-1.
Find elsewhere
🌐
Java-forums
java-forums.org › new-java › 35335-removing-last-character-string.html
Removing last character from a String
November 28, 2010 - I have a string which has alot of commas. What I want to do is remove the last comma in the String for example: 1,2,3,4,5, I want it to be 1,2,3,4,5 I
🌐
OutSystems
outsystems.com › forums › discussion › 85690 › how-to-remove-last-comma-from-the-string
How to remove last comma from the string | OutSystems
February 10, 2023 - In string first check do we have last comma or not if have then remove the last comma from the string · eg. Table,Chair,Ball, - here last comma remove from this string
🌐
Learn IT University
learn-it-university.com › home › clean up your comma-separated list by removing trailing commas
Clean Up Your Comma-Separated List by Removing Trailing Commas - Learn IT University
June 22, 2024 - When working with comma-separated strings in Java, it's common to encounter scenarios where a trailing comma at the end of the string needs to be removed. This may be necessary to ensure proper data formatting and handling within your programs. Fortunately, there are efficient ways to achieve this, such as using replaceAll method in Java.