You can use:

String str1 = str.replaceAll("[.]", "");

instead of:

String str1 = str.replaceAll(".", "");

As @nachokk said, you may want to read something about regex, since replaceAll first parameter expects for a regex expression.

Edit:

Or just this:

String str1 = s.replaceAll("[,.]", "");

to make it all in one sentence.

Answer from Christian Tapia on Stack Overflow
Discussions

java - How to remove all special character in a string except dot and comma - Stack Overflow
I have a sentence with many special characters and text in it, I want remove all the special characters except dot and comma. For example, this is what have: [u' %$HI# Jhon, $how$ are *&$%y... More on stackoverflow.com
🌐 stackoverflow.com
September 24, 2016
java - Remove trailing comma from comma-separated string - Stack Overflow
I got String from the database which have multiple commas (,) . I want to remove the last comma but I can't really find a simple way of doing it. What I have: kushalhs, mayurvm, narendrabz, What ... More on stackoverflow.com
🌐 stackoverflow.com
string - How to replace comma (,) with a dot (.) using java - Stack Overflow
I am having a String str = 12,12 I want to replace the ,(comma) with .(Dot) for decimal number calculation, Currently i am trying this : More on stackoverflow.com
🌐 stackoverflow.com
java - remove comma and some characters from a string - Stack Overflow
I have a string that can be in any of the format like below: 'xyz','abc' //after conversion --> 'abc' 'abc','xyz' //after conversion --> 'abc' 'xyz' //after More on stackoverflow.com
🌐 stackoverflow.com
🌐
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....
🌐
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".
🌐
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.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › remove punctuation from a string in java
Remove Punctuation From a String in Java | Baeldung
August 22, 2023 - Learn how to remove punctuation from a string using the standard String.replaceAll() method.
🌐
RoseIndia
roseindia.net › answers › viewqa › Java-Beginners › 33087-java-replace-dot-with-comma.html
java replace dot with comma
java replace dot with comma java replace dot with comma Hi, How to replace dot with command in Java program? thanks Hi, Here is simple example for replacing the dot(.) with comma: package test.app; public class JavaStringExample ... find and replace in java find and replace in java Need 'find and replace' function in Java to find special keyword in XMl such as @,#,!,%..and replace with their corresponding entities · How to split string in Java using comma?
Top answer
1 of 2
1

You could chain some String::replace methods

String str = "'abc','xyz','abc'";
str = str.replace (",'xyz'", "").replace("'xyz',", "").replace("'xyz'", "");

output

'abc','abc'

2 of 2
0

Regex alone is not very well equipped for this task. However, you can do the following:

  1. Split your string on comma. In fact, you can split by comma surrounded by any amount of whitespace, in case the sting you get is "'abc', 'xyz'", for example. You can use the regex \s*,\s* for that.
  2. Remove anything that matches the string you don't want - you can use basic string matching or regex for more complex patterns.
  3. Convert the split string into a comma separated list again.

Usign Java 8 Stream operations, this can be short:

String[] split = str.split(("\\s*,\\s*")); //1. split into separate strings
String result = Arrays.stream(split) //turn to stream
        .filter(chunk -> !"'xyz'".equals(chunk)) //2. remove anything you don't want
        .collect(Collectors.joining(",")); //3. convert back to comma separated list

The .filter predicate can be changed to whatever suits you - if you only want to match "'abc'" then you can use chunk -> "abc'".equals(chunk) or you can use .contains or equalsIgnoreCase, or even regex.


It might be worth extracting the filter rule out into a separate Predicate, so you can more easily change it, when needed

Predicate<String> filterRule = chunk -> !"'xyz'".equals(chunk);

/* ... */
  .filter(filterRule)
/* ... */

Although if we examine this "'xyz'".equals is already a predicate itself, and predicates can be negated, so you don't need to write a whole lambda for this but just re-use the methods as functional interfaces:

Predicate<String> stuffWeDontWant = "'xyz'"::equals;
Predicate<String> filterRule = stuffWeDontWant.negate();

This can all be inlined but it's a bit ugly, as you have to cast it into the proper predicate to negate it

Predicate<String> filterRule = ((Predicate<String>) "'xyz'"::equals).negate();

The final thing can look like this:

Predicate<String> filterRule = ((Predicate<String>) "'xyz'"::equals).negate();

String result = Arrays.stream(str.split(("\\s*,\\s*")))
    .filter(filterRule)
    .collect(Collectors.joining(","));

So now you can more easily change whatever your filter rule is. You can even re-use this for any kind of list by just passing different predicate to use as a filter.


A non-Java 8 or non-Stream way to do the same to do the following:

String[] split = str.split(("\\s*,\\s*")); //1. split into separate strings

List<String> list = new ArrayList<>(Arrays.asList(split));//convert into an ArrayList to allow removing

list.removeAll(Collections.singleton("'xyz'"));//2. remove anything you don't want
String result = String.join(",", list); //3. convert back to comma separated list

This approach can still be mixed with using a Predicate by using .removeIf() but this time it has to match exactly what you don't want:

list.removeIf("'xyz'"::equals);
/* or */
list.removeIf("'xyz'"::equalsIgnoreCase);
/* or */
list.removeIf("'xyz'"::startsWith);
/* or */
list.removeIf("'xyz'"::contains);
/* or */
list.removeIf("'xyz'"::endsWith);
/* or */
list.removeIf(Pattern.compile("'(xy?z)*'").asPredicate());
/* ...and so on... */
🌐
Delft Stack
delftstack.com › home › howto › java › how to remove punctuation from string in java
How to Remove Punctuation From String in Java | Delft Stack
February 2, 2024 - We then call the removePunctuation method, pass the input string, and store the result in the result variable. Using a custom regular expression allows for fine-tuning the punctuation removal process according to specific requirements.
🌐
Reddit
reddit.com › r/javahelp › need help removing commas and a specific string from a string arraylist
r/javahelp on Reddit: Need help removing commas and a specific string from a string arraylist
February 16, 2015 -

import java.io.File; import java.util.ArrayList; import java.util.Scanner;

public class EmailReader {

    public static void main(String[] args) throws Exception {

            Scanner inputFile = new Scanner(new File("NameList.csv"));

            inputFile.nextLine();

            ArrayList<String> studentNames = new ArrayList();
            ArrayList<String> emails = new ArrayList();
            String ender = "@virginia.edu";

            while (inputFile.hasNext()) {

                    String name = inputFile.nextLine().concat("@virginia.edu");

                    studentNames.add(name);
            }
            for (int i = 0; i < studentNames.size(); i += 2) {

                    emails.add(studentNames.get(i));
                    ;

            }
            System.out.println(emails);

    }

}

🌐
Quora
quora.com › How-can-commas-be-removed-from-an-integer-value-in-Java
How can commas be removed from an integer value in Java? - Quora
Answer: An int value contains a given number of bits - it does not have array of characters for a comma to be represented in, and so commas can’t exist to be removed in an int or Integer in java. If you want to parse a String to remove commas before trying to turn it into an int value, try [cod...
🌐
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 - We pass the characters of the string that needs to be replaced with the updated character. ... import java.util.*; import java.lang.*; public class comma { public static void main(String []args) { String str="h,e,l,l,o,"; System.out.println(str.replaceAll(",$","")); } }
🌐
Stack Overflow
stackoverflow.com › questions › 53642182 › removing-a-comma-from-a-string
java - Removing a comma from a string - Stack Overflow
To your first comment, why would you iterate through the map? The string sentence is what you’re trying to solve. ... You can use regular expressions to extract all words (consisting only of letters), and then search the map as you wish. I know that you ask only for comma, but I assume this is the use-case.