Remove Comma from String in Java

Learn about how to remove comma from String in java.

Remove comma from String in java

1. Using replace() method

You can use String’s replace() method to remove commas from String in java.

Here is syntax of replace method:

Output:

India China Bhutan

2. Using replaceAll() method

You can also use replaceAll() method to remove comma from String in java. It is similar to replace() method, but it takes regex as argument. You can see difference between replace and replaceAll over here.

Here is syntax of replaceAll method:

Output:

India China Bhutan

Remove Last Comma from String in Java

If you want to remove comma at the end of the string, then it is similar to removing last character from String in java.

1. Using substring() method

We can use String’s substring() method to remove last comma from String in java. Here we will use substring() method with 2 parameters.

Here,
start index: 0
end index:str.length()-1

Please note that this method is not thread safe and it will fail with empty or null String.
So here is java program to remove last character from String in java.

Output:

India, China, Bhutan

2. Using replaceAll() method

We can make use of Regular expression also to remove last character from String in java.

Output:

India, China, Bhutan

3. Using StringBuffer/StringBuilder

We can StringBuffer's deleteCharAt() to remove last comma from String in java.

Output:

India, China, Bhutan

4. Using StringUtil’s chop() method

You need to add below dependency to get Apache commons lang jar.

and then use StringUtils.chop() method to remove last comma from String in java,

Output:

India, China, Bhutan

Remove comma from number in java

You can use String’s replace() or replaceAll() method to remove comma from number in java.

Output:

Using replace: 1232000100
Using replaceAll: 1232000100

That’s all about how to remove comma from String in Java.

Was this post helpful?

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.