Remove Trailing Comma from Comma-Separated String

Remove trailing comma from comma-separated string

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.

how to remove comma if string having comma at end

You could try a regular expression:

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

Or a simple substring:

if (names.endsWith(",")) {
names = names.substring(0, names.length() - 1);
}

Remove trailing comma

How about using replace as below

SELECT SUBSTRING(Name, CHARINDEX(',', Name) + 1, LEN(Name)) + ' ' +
REPLACE(SUBSTRING(Name, 1, CHARINDEX(',', Name)),',','') AS NewName
FROM Employees

How to remove a trailing comma from a string (Java)

I'm not sure the constraints of this project for your course, but if you're able, try a StringJoiner! It will add a delimiter automatically to separate items that are added to it. Another note, I think you're going to want to declare your String outside of your for loop. otherwise it resets every iteration.

StringJoiner joiner = new StringJoiner(",");
for(int number : array){
joiner.add(String.valueOf(number));
}
System.out.print(joiner.toString());

Remove Last Comma from a string

This will remove the last comma and any whitespace after it:

str = str.replace(/,\s*$/, "");

It uses a regular expression:

  • The / mark the beginning and end of the regular expression

  • The , matches the comma

  • The \s means whitespace characters (space, tab, etc) and the * means 0 or more

  • The $ at the end signifies the end of the string

How do I remove the last comma from a string using PHP?

Use the rtrim function:

rtrim($my_string, ',');

The Second parameter indicates the character to be deleted.

Removing last comma every line

You have to use replaceAll() instead of replace() --> resultString = resultString.replaceAll(",$", ""); because replaceAll() takes regex where $ means end of input.

Example :

public static void main(String[] args) {
String s = "abc,def,";
System.out.println(s.replaceAll(",$", ""));
}

O/P :

abc,def

EDIT : Based on your code.

You should probably change your code to :

 for ( String data1: overall.keySet() ){
resultString += data1 + "," + overall.get(data1).toString() + System.getProperty("line.separator");
resultString = resultString.replaceAll(",$", "");
}


Related Topics



Leave a reply



Submit