Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace special characters in a string?

Tags:

java

string

I have a string with lots of special characters. I want to remove all those, but keep alphabetical characters.

How can I do this?

like image 902
Tanu Avatar asked Nov 26 '10 07:11

Tanu


People also ask

How do you replace special characters in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")


1 Answers

That depends on what you mean. If you just want to get rid of them, do this:
(Update: Apparently you want to keep digits as well, use the second lines in that case)

String alphaOnly = input.replaceAll("[^a-zA-Z]+",""); String alphaAndDigits = input.replaceAll("[^a-zA-Z0-9]+",""); 

or the equivalent:

String alphaOnly = input.replaceAll("[^\\p{Alpha}]+",""); String alphaAndDigits = input.replaceAll("[^\\p{Alpha}\\p{Digit}]+",""); 

(All of these can be significantly improved by precompiling the regex pattern and storing it in a constant)

Or, with Guava:

private static final CharMatcher ALNUM =   CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))   .or(CharMatcher.inRange('0', '9')).precomputed(); // ... String alphaAndDigits = ALNUM.retainFrom(input); 

But if you want to turn accented characters into something sensible that's still ascii, look at these questions:

  • Converting Java String to ASCII
  • Java change áéőűú to aeouu
  • ń ǹ ň ñ ṅ ņ ṇ ṋ ṉ ̈ ɲ ƞ ᶇ ɳ ȵ --> n or Remove diacritical marks from unicode chars
like image 132
Sean Patrick Floyd Avatar answered Sep 29 '22 23:09

Sean Patrick Floyd