I've a csv string like
"abc, java, stackoverflow , stack exchange , test"
Can I use regex to remove the space around the commas to get a string like
"abc,java,stackoverflow,stack exchange,test"
regex package for searching and replacing matching characters or substring. Since String is immutable in Java it cannot be changed, hence this method returns a new modified String. If you want to use that String you must store it back on the relevant variable.
They can be used to search, edit, or manipulate text and data. The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.
str = str.replaceAll("\\s*,\\s*", ",");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With