You can use this :

String str = "A,  Category,   \"Agriculture, forestry and fishing\",";
String result = str.replaceAll(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "");
//------------------------------^--------------------------------------
System.out.println(result);

This will print :

A,Category,"Agriculture, forestry and fishing",
//----------------------^---------------------

The space inside the quotes is not changes, just outside the quotes.


Here is a code DEMO and here is a regex DEMO


EDIT

This part is from @Exception_al so he suggest to use this :

String result = str.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");

This solution is very good if you want to replace all spaces between comma , and the word.

You can check how this can work in regex DEMO

Answer from Youcef LAIDANI on Stack Overflow
🌐
Programming.Guide
programming.guide › java › remove-trailing-comma-from-comma-separated-string.html
Java: Removing trailing comma from comma separated string | Programming.Guide
String str = "lorem, ipsum, dolor, "; str = str.replaceAll(", $", ""); System.out.println(str); // "lorem, ipsum, dolor" , $ is a regular expression that means "comma, followed by a space, followed by end of string".
🌐
Coderanch
coderanch.com › t › 451947 › java › remove-spaces
How to remove spaces (Java in General forum at Coderanch)
June 30, 2009 - Once you have that established, the rest is easy. it SOUNDS like what you want is to replace every occurrence of " , " (space-comma-space) with "," (comma). Note that regular expressions in java can be a little tricky, but it should be able to do what you want... There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... Another way to do it would be to split the String on the ",", trim each element of the resulting String[] and rewrite the values to a comma separated String.
🌐
Medium
medium.com › @AlexanderObregon › splitting-a-string-by-comma-in-java-the-right-way-1d3aa2aff1fa
Splitting a String by Comma in Java the Right Way | Medium
June 26, 2025 - Learn how Java splits comma-separated strings, trims spaces, handles regex, and safely processes input in arrays and lists without common parsing errors.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › split a comma-separated string in java
Split a comma-separated String in Java - Apps Developer Blog
September 6, 2022 - // String with whitespaces after commas String names = "Tom, Steve ,John, Megan, Melissa"; // split the string and remove any whitespace List<String> = new ArrayList<>(Arrays.asList(names.split("\\s*,\\s*"))); It also can be done using the map() and collect() operators from the Java 8 Streams API.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › core java › remove comma from string in java
Remove Comma from String in Java - Java2Blog
February 2, 2022 - You can use String’s replace() method to remove commas from String in java.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 12 › how-to-split-comma-separated-string-in-java-example.html
How to split a comma separated String in Java? Regular Expression Example
Just remember that it takes regular ... If there is space between values then you can use the regular expression "\\s*,\\s*" to remove the space around individual values. Other Java String tutorials from this blog...
🌐
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.
🌐
CodingTechRoom
codingtechroom.com › question › remove-unused-spaces-commas-java
How to Remove Unused Spaces and Commas from a String in Java? - CodingTechRoom
"; String cleanedString = originalString.replaceAll("\s+,", ",").replaceAll(",\s+", ",").replaceAll("\s+", " ").trim(); System.out.println(cleanedString); // Outputs: "Java, is fun, learning!" ... Use regular expressions to identify and replace unwanted spaces and commas.
🌐
Java67
java67.com › 2016 › 01 › how-to-split-string-by-comma-in-java-with-example.html
How to split String by comma in Java - Example Tutorial | Java67
Similar to the earlier example, you can use the split() method to accomplish this task. Just pass a "," instead of "\\s+" the regular expression to remove whitespaces, as the regular expression.
🌐
Stack Overflow
stackoverflow.com › questions › 68303938 › remove-space-after-comma-in-string-regex-java
Remove space after comma in String regex java - Stack Overflow
July 8, 2021 - Looks like CSV. Why not read the CSV in with a CSV parser and strip all of the cells? A regex solution won't work if some cells have quoted data with spaces that aren't padding around delimiters.
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter. We can achieve the same result by using Java 8 Stream features: String[] splitted = Arrays.stream(i...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › trim
Java String trim() - Remove Leading/Trailing Spaces | Vultr Docs
December 30, 2024 - Example: Parsing a comma-separated values (CSV) file with unwanted spaces around values. ... Trimming before splitting ensures that the job title does not carry trailing spaces, which could affect further data manipulations or display. Use trim() to maintain the aesthetics and correctness of user-generated content and system messages. Example: Preparing a statement for display or printing. ... String announcement = " New store opening soon!
🌐
Java2Blog
java2blog.com › home › core java › how to replace comma with space in java
How to Replace Comma with Space in Java - Java2Blog
September 15, 2022 - As you can see, replace() method replaced each comma with space in the String. Use replaceAll() method to replace comma with space in java. It is identical to replace() method, but it takes regex as argument.