Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

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,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I would use a character class. Include what should be there and then negate it.
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Include what should be there and then negate it.

Unless of course the garbage really is limited and predictable
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikotesh
srikotesh

ASKER

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,
Could you please explain with example i didn't understand.
Firstly, can you tell me whether the only special characters will ever be '+' and '%'?
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.
Thanks gurpsbassi,krishna.
Cehj,
From my string I can see only % and +.
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.
Hi Cehj,
If u see krishna kumar statement that will give solution for all special characters.
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)
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.
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

Thanks for your suggestion..
Actually, you want spaces, so just "[^\\w]" or "[^a-zA-Z]" is you're worried about underscores