How to remove special characters from a string?

I want to remove special characters, for example:

- + ^ . : , 

from a string using Java.

+63
java regex
Sep 26 2018-11-11T00:
source share
8 answers

It depends on what you define as special characters, but try replaceAll(...) :

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

Please note that the ^ character should not be the first in the list, as you will either need to avoid it, or it will mean "any other than these characters."

One more note: the symbol - must be the first or last in the list, otherwise you would have to avoid it or define a range (for example :-, means "all characters in the range" : to , ).

So, in order to maintain consistency and not depend on the character’s positioning, you can avoid all the characters that are of particular importance in regular expressions (the following list is not complete, so keep in mind other characters such as ( , { , $ , etc. .):

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


If you want to get rid of all punctuation and characters, try this regular expression: \p{P}\p{S} (keep in mind that in Java strings you would need to avoid backslashes: "\\p{P}\\p{S}" ).

The third way could be something like this if you can pinpoint what should be left on your line:

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

This means: replace anything that is not a symbol of the word (az in any case, 0-9 or _) or spaces.

Edit: Please note that there are several other templates that may be useful. However, I cannot explain them all, so look at the regular-expressions.info link.

Here's a less restrictive alternative to the "define allowed characters" approach suggested by Ray:

 String result = yourString.replaceAll("[^\\p{L}\\p{Z}]",""); 

The regular expression matches all that is not a letter in any language, and not a separator (spaces, lines, etc.). Note that you cannot use [\P{L}\P{Z}] (upper case P means no this property), because it means "everything that is not a letter or a space", which almost matches all, as letters are not spaces and vice versa.

Additional Unicode Information

Some Unicode characters seem to cause problems due to various possible ways to encode them (as a single code point or a combination of code points). See regular-expressions.info for more details.

+194
Sep 26 '11 at 8:11
source share

Try the replaceAll() method of the String class.

By the way, this is a method, return type and parameters.

 public String replaceAll(String regex, String replacement) 

Example:

 String str = "Hello +-^ my + - friends ^ ^^-- ^^^ +!"; str = str.replaceAll("[-+^]*", ""); 

It should remove all the characters {'^', '+', '-'} that you want to delete!

+13
Sep 26 '11 at 8:32
source share

As described here http://developer.android.com/reference/java/util/regex/Pattern.html

Patterns are compiled by regular expressions. Convenient methods such as String.matches , String.replaceAll and String.split will be preferred in many cases, but if you need to work a lot with the same regular expression, it may be more efficient to compile it once and reuse It. The Pattern class and its companion Matcher also offer more functionality than the small amount open by String.

 public class RegularExpressionTest { public static void main(String[] args) { System.out.println("String is = "+getOnlyStrings("!&(*^*(^(+one(&(^()(*)(*&^%$#@!#$%^&*()(")); System.out.println("Number is = "+getOnlyDigits("&(*^*(^(+91-&*9hi-639-0097(&(^(")); } public static String getOnlyDigits(String s) { Pattern pattern = Pattern.compile("[^0-9]"); Matcher matcher = pattern.matcher(s); String number = matcher.replaceAll(""); return number; } public static String getOnlyStrings(String s) { Pattern pattern = Pattern.compile("[^az AZ]"); Matcher matcher = pattern.matcher(s); String number = matcher.replaceAll(""); return number; } } 

Result

 String is = one Number is = 9196390097 
+12
Oct. 06 '15 at 5:53 on
source share

Use String.replaceAll() method in Java. replaceAll should be good enough for your problem.

+2
Sep 26 2018-11-11T00:
source share

Removing Specail Character

String t2 = "! @ # $% ^ & * () - ';, /?> <+ Abdd";

t2 = t2.replaceAll ("\\ W +", "");

The output will be: abdd.

This works great.

+2
Sep 04 '17 at 7:11
source share

You can remove a single char as follows:

 String str="+919595354336"; String result = str.replaceAll("\\\\+",""); System.out.println(result); 

OUTPUT:

 919595354336 
+1
Dec 19 '16 at 19:26
source share

If you just want to replace the letter in java, use Pattern.quote(string) to avoid any string in the literal.

 myString.replaceAll(Pattern.quote(matchingStr), replacementStr) 
0
Jul 26 '17 at 14:55
source share

This will replace all characters except alphanumeric

 replaceAll("[^A-Za-z0-9]",""); 
0
Dec 08 '17 at 7:28
source share



All Articles