How to replace special characters in a string?

I have a string with a lot of special characters. I want to remove all of these, but keep the alphabetic characters.

How can i do this?

+77
java string
Nov 26 2018-10-11T00:
source share
9 answers

It depends on what you mean. If you just want to get rid of them, do the following:
(Update: apparently you also want to keep the numbers, use the second line in this case)

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

or equivalent:

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

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

Or, 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 reasonable that is still ascii, look at these questions:

  • Convert Java string to ASCII
  • Java change in aeouu
  • ล„ วน ลˆ ลˆ แน… ล† แน‡ แน‹ แน‰ ฬˆ ษฒ ฦž แถ‡ ษณ ศต โ†’ n or Remove diacritics from Unicode characters
+170
Nov 26 '10 at 7:44
source share

I am using this.

 s = s.replaceAll("\\W", ""); 

It replaces all special characters from the string.

Here

\ w: word character abbreviated for [a-zA-Z_0-9]

\ W: character without a word

+59
Feb 28 '13 at 10:13
source share

You can use the following method to store alphanumeric characters.

 replaceAll("[^a-zA-Z0-9]", ""); 

And if you want to keep only alphabetic characters, use this

 replaceAll("[^a-zA-Z]", ""); 
+9
Nov 29 '14 at 5:48
source share
 string Output = Regex.Replace(Input, @"([ a-zA-Z0-9&, _]|^\s)", ""); 

Here all special characters except space, comma and ampersand are replaced. You can also omit the space, comma, and ampersand with the following regular expression.

 string Output = Regex.Replace(Input, @"([ a-zA-Z0-9_]|^\s)", ""); 

Where Input is the string we need to replace the characters.

+1
Mar 03 '15 at 5:30
source share

Replace any special characters with

 replaceAll("\\your special character","new character"); 

for example: replace all occurrences * with a space

 replaceAll("\\*",""); 

* this statement can replace only one type of special character at a time

+1
Aug 09 '18 at 4:33
source share

Following the example of Andrzej Doyle's answer , I think the best solution would be to use org.apache.commons.lang3.StringUtils.stripAccents() :

 package bla.bla.utility; import org.apache.commons.lang3.StringUtils; public class UriUtility { public static String normalizeUri(String s) { String r = StringUtils.stripAccents(s); r = r.replace(" ", "_"); r = r.replaceAll("[^\\.A-Za-z0-9_]", ""); return r; } } 
+1
Apr 14 '19 at 2:31
source share

You can use basic regular expressions for strings to find all special characters, or use pattern classes and classes to find / modify / delete user-defined strings. This link contains some simple and straightforward examples for regular expressions: http://www.vogella.de/articles/JavaRegularExpressions/article.html

0
Nov 26 '10 at 9:36
source share

You can get the unicode for this garbage symbol from the charactermap tool in the pc window and add \ u e.g.. \ U00a9 for the copyright symbol. Now you can use this line with this particular junk caharacter, not delete the junk email character, but replace it with the corresponding unicode.

0
Aug 26 '14 at 8:05
source share

For spaces use "[^ az AZ 0-9]" this pattern

0
Apr 16 '18 at 14:52
source share



All Articles