how to remove special characters in a string

Hi Experts,

How do i remove special characters in a string.

String description = "Yeahh, I have no a idea %what's hap+pening now%Yeahh, I have no a idea what's happe+ning now";
From the above string i want to remove % and + symbols
By using replace all i can remove % symbol, how to remove both % and + symbols ?
String desc1 = description.replaceAll("%", " ");

Can some one suggest me.
Thanks,
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
I would use a character class. Include what should be there and then negate it.
0
gurpsbassiCommented:
maybe something like:
final String desc1 = description.replaceAll("%|\\+", " ");
0
CEHJCommented:
Include what should be there and then negate it.

Unless of course the garbage really is limited and predictable
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

krishna kishore mellacheruvu venkataCommented:
Use Regular expressions some thing like this

String result = yourString.replaceAll("[-+.^:,]","");

and try.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
Hi gurpsbassi,
By using above statement i can able to remove both special characters successfully.

Hi CEHJ,

Could you please explain with example i didn't understand.


Thanks,
0
CEHJCommented:
Could you please explain with example i didn't understand.
Firstly, can you tell me whether the only special characters will ever be '+' and '%'?
0
gurpsbassiCommented:
Could you please explain with example i didn't understand

String.replaceAll(..) takes a regular expression as its first argument. So I supplied
"%|\\+" Which matches % character and + character. I had to escape the + character by using backslash because it is a special regex character used as a quantifier for regex.
0
srikoteshAuthor Commented:
Thanks gurpsbassi,krishna.
Cehj,
From my string I can see only % and +.
0
CEHJCommented:
From my string I can see only % and +.
That doesn't really answer my question ;)

The point is, an inclusive regex will cause the code to fail if, say, another special character gets introduced. An exclusive one is safer.
0
srikoteshAuthor Commented:
Hi Cehj,
If u see krishna kumar statement that will give solution for all special characters.
0
CEHJCommented:
If u see krishna kumar statement that will give solution for all special characters.

That won't remove '%' but will remove ',' (which you showed you don't want)
0
srikoteshAuthor Commented:
Sorry there is correction in my description there is no comma in my desription

String description = "Yeahh I have no a idea %what's hap+pening now%Yeahh I have no a idea what's happe+ning now";

even though if it is having , also i want to remove.

I am able to remove special characters..
String result = yourString.replaceAll("[%-+.^:,]","");
the above line will do all the things.
0
CEHJCommented:
The accepted answer won't work, for the reason i gave.

It would help in future if you actually specify the requirement properly, rather than waiting until your final post.
From that, the following will suit more simply:

s = s.replaceAll("[^\\w\\s]", "");

Open in new window

0
srikoteshAuthor Commented:
Thanks for your suggestion..
0
CEHJCommented:
Actually, you want spaces, so just "[^\\w]" or "[^a-zA-Z]" is you're worried about underscores
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.