简体   繁体   English

从字符串java中删除所有特殊字符

[英]remove all special character from string java

I wanted to know how I can do to completely eliminate ALL of the special characters from a string. 我想知道如何完全消除字符串中的所有特殊字符。 In other words I would leave only the words, thus eliminating any other characters as +-òç@èé etc. 换句话说,我只留下单词,从而消除任何其他字符为+-òç@èé等。

Now i use 现在我用

myString =  Normalizer.normalize(myString, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

But some characters speacials still remain. 但是一些人物仍然存在。

Replace the \\p{ASCII} regex class with a stricter set that only contains the chars you allow. \\p{ASCII} regex类替换为仅包含允许的字符的更严格集。 For example, 例如,

myString =  Normalizer.normalize(myString, Normalizer.Form.NFD).replaceAll("[^a-zA-Z]", "");

will first decompose accented chars like é to two parts e + combining ´ (normal form D) and then the regex will remove any character that is not ASCII a..z or A..Z. 将首先将é等重音字符分解为两部分e + combining ´ (正常形式D),然后正则表达式将删除任何不是ASCII a..z或A..Z的字符。

The default charset is unicode (utf-8) in java ,The below code uses the unicode representation of a character and checks if the unicode of a character is speicial character; java中的默认字符集是unicode(utf-8),下面的代码使用字符的unicode表示,并检查字符的unicode是否为speicial字符; The solution given below is of the Time complexity = O(n); 下面给出的解是时间复杂度= O(n);

public class RemoveSpecialCharacters {

/**
 * @param args the command line arguments
 */

private static boolean isSpecialCharacter(int b)
{
    if((b>=32 && b<=47 )||(b>=58 && b<=64)||(b>=91 && b<=96) ||(b>=123 && b<=126)||b>126)
        return true;
    return false;


}
public static String removeSpecialCharacters(String a)
{
    StringBuffer s=new StringBuffer(a);


    int lenvar=s.length();
    String myString="";
    for(int i=0;i<lenvar;i++)
    {


        if(!isSpecialCharacter(s.charAt(i)))
        {
            myString+=s.charAt(i);


        }

    }
    return myString;


}


public static void main(String[] args) {
   System.out.println(removeSpecialCharacters("fleCKHE)_+_+"));



}


}

o/p:fleCKHE O / P:fleCKHE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM