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 expressionThe
,
matches the commaThe
\s
means whitespace characters (space, tab, etc) and the*
means 0 or moreThe
$
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
How to Send Date in Rest API in Post Method
Letting a Button Blink White to Red, Then Giving Its Old, Rounded Form
How a Jar File Can Read an External Properties File
Enter Array Without Knowing Its Size
Java 8 Convert Utc Time to Edt/Est So That Date Remains the Same
Is There a Simpler Way to Check Multiple Values Against One Value in an If-Statement
What Is the Most Elegant Way to Check If All Values in a Boolean Array Are True
How to Format a String Number to Have Commas and Round
Spring Data JPA How to Locate Attribute With the Given Name
Expiry Time @Cacheable Spring Boot
How to Convert a Kotlin Source File to a Java Source File
How to Fill Hashmap from Java Property File With Spring @Value
Save Child Objects Automatically Using JPA Hibernate
How to Process a Multipart Request Consisting of a File and a Json Object in Spring Restful Service
How to Sort Integer Digits in Ascending Order Without Strings or Arrays
How to Get Multiple Columns from Table Using Jpa
How to Enable Request Scope in Async Task Executor
Netbeans Tomcat Build Failed - the Module Has Not Been Deployed