Dunkey Dunkey - 1 year ago 102
Java Question

Remove apostrophe and white space from string

So I'm playing around string manipulation. I'm done replacing white space characters with hyphens. Now I want to combine replacing white spaces characters and removing apostrophe from string. How can I do this?

This is what I've tried so far:

String str = "Please Don't Ask Me";
String newStr = str.replaceAll("\\s+","-");

System.out.println("New string is " + newStr);

Output is:


But I want the output to be:


But I can't get to work removing the apostrophe. Any ideas? Help is much appreciated. Thanks.

Answer Source

Try this:

String newStr = str.replaceAll("\\s+","-").replaceAll("'", "");

The first replaceAll returns the String with all spaces replaced with -, then we perform on this another replaceAll to replace all ' with nothing (Meaning, we are removing them).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download