• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Delete Special Charecters form the String

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a String s1 = "abc@!&def(*)";
I want to delete all the special charecters from the String s1.
The expected result should be s1 = "abcdef"

How can i achive this?

Thanks,
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1.replaceAll("[^\\w]", "")

This will look for all substrings matching the regular expression "[^\w]" which means everything except a word character (0-9, a-z, A-Z and _), and then replace them with the empty string.

If you thing that a word character is still too lenient, you can use "[^a-zA-Z0-9]", or even "[^a-zA-Z]".
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

Thanks a lot. It worked like charm!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic