How do I replace a string in Java that contains dot?

I need to replace the string that contains spaces and periods I tried the following code:

String customerName = "Mr. Raj Kumar";

customerName = customerName.replaceAll(" ","");
System.out.println("customerName"+customerName);

customerName = customerName.replaceAll(".","");
System.out.println("customerName"+customerName);

But the result is:

and

I got the correct customer name from the first SOP, but I didn't get any value from the second SOP

Solution

Escape point, otherwise it will match any role This escape is necessary because replaceall () treats the first parameter as a regular expression

customerName = customerName.replaceAll("\\.","");

You can do the whole thing with one statement:

customerName = customerName.replaceAll("[\\s.]","");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>