Table of Contents
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:
1 2 3 |
public String replace(CharSequence target, CharSequence replacement) |
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class RemoveCommaFromStringMain { public static void main(String[] args) { String str = "India, China, Bhutan, "; str = str.replace(",",""); System.out.println(str); } } |
Output:
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:
1 2 3 |
public String replaceAll(String regex, String replacement) |
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class RemoveCommaFromStringMain { public static void main(String[] args) { String str = "India, China, Bhutan, "; str = str.replaceAll(",",""); System.out.println(str); } } |
Output:
Further reading:
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.
1 2 3 |
public String substring(int startIndex, int endIndex) |
Here,
start index: 0
end index:str.length()-1
1 2 3 |
str.substring(0, 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class RemoveLastCommaStringMain { public static void main(String[] args) { RemoveLastCommaStringMain rlc=new RemoveLastCommaStringMain(); String str="India, China, Bhutan,"; str=rlc.removeLastChar(str); System.out.println(str); } private String removeLastChar(String str) { return str.substring(0, str.length() - 1); } } |
Output:
2. Using replaceAll() method
We can make use of Regular expression also to remove last character from String in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; public class RemoveLastCommaStringMain { public static void main(String[] args) { RemoveLastCommaStringMain rlc=new RemoveLastCommaStringMain(); String str="India, China, Bhutan,"; str=str.replaceAll(",$",""); System.out.println(str); } } |
Output:
3. Using StringBuffer/StringBuilder
We can StringBuffer's
deleteCharAt()
to remove last comma from String in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; public class RemoveLastCommaStringMain { public static void main(String[] args) { RemoveLastCommaStringMain rlc = new RemoveLastCommaStringMain(); String str="India, China, Bhutan,"; str = rlc.removeLastChar(str); System.out.println(str); } private String removeLastChar(String str) { StringBuilder sb = new StringBuilder(str); sb.deleteCharAt(sb.length() - 1); return sb.toString(); } } |
Output:
4. Using StringUtil’s chop() method
You need to add below dependency to get Apache commons lang jar.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
and then use StringUtils.chop()
method to remove last comma from String in java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.java2blog; import org.apache.commons.lang3.StringUtils; public class RemoveLastCharStringMain { public static void main(String[] args) { String str="java2blog"; str=StringUtils.chop(str); System.out.println(str); } } |
Output:
Remove comma from number in java
You can use String’s replace()
or replaceAll()
method to remove comma from number in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; public class RemoveCommaFromStringMain { public static void main(String[] args) { String str1 = "123,2000,100"; str1 = str1.replace(",",""); System.out.println("Using replace: "+str1); String str2 = "123,2000,100"; str2 = str2.replace(",",""); str2 = str2.replaceAll(",",""); System.out.println("Using replaceAll: "+str2); } } |
Output:
Using replaceAll: 1232000100
That’s all about how to remove comma from String in Java.