简体   繁体   English

如何从字符串中删除多个特殊字符,而无需任何内置函数,例如下面的jdk 1.4的replaceAll()

[英]How to remove multiple special characters from a String without any inbuilt functions like replaceAll() for jdk 1.4 below

Using the below function i am able to simply remove a single special character, need multiple types of them to be removed from a single String. 使用以下功能,我能够简单地删除单个特殊字符,需要从单个String中删除多种特殊字符。

static public String replaceAll(String str, String replace, String replacement )  
{       
StringBuffer sb = new StringBuffer( str );  
int firstOccurrence = sb.toString().indexOf( replace );  

while( firstOccurrence != -1 )   
{     
sb.replace( firstOccurrence, firstOccurrence + replace.length(), replacement );  
         firstOccurrence = sb.toString().indexOf( replace );  
}  

return sb.toString();   
} 

Needs this so it works below jdk 1.4 without all the inbuilt function like replaceAll() 需要此功能,以便它在jdk 1.4以下版本中运行而无需所有内置函数,例如replaceAll()

Thanks. 谢谢。

What I would do is remove all special characters, such as control characters, non-ASCII special characters as well as the ASCII ones like this. 我要做的是删除所有特殊字符,例如控制字符,非ASCII特殊字符以及像这样的ASCII字符。

public static String stripSpecialCharacters(String str) {
   StringBuffer sb = new StringBuffer();
    for(int i=0;i<str.length();i++) {
        char ch = str.charAt(i);
        if (Character.isLetterOrDigit(ch) || Character.isSpaceChar(ch))
            sb.append(ch);
    }
    return sb.toString();
}

Note: you want to avoid creating multiple characters or re-arranging characters in a StringBuffer as these become O(N^2) operations in this context. 注意:您要避免在StringBuffer中创建多个字符或重新排列字符,因为在这种情况下,这些字符将变为O(N ^ 2)操作。

Edit: Your question title says that you want to replace multiple characters, , i have used a simple method to replace multiple special characters in "replace" by a specified character.. 编辑:您的问题标题说,您想替换多个字符,我已经用一种简单的方法用指定字符替换“替换”中的多个特殊字符。

EDIT: 编辑:

static public String replaceAll(String str, String replace, char replacement )  
{    
String T=""; 
int flag=0;  
for(int i=0;i<str.length();i++)
{
      char a=str.charAt(i);
      for(int j=0;j<replace.length();j++)
      {
          if(a==replace.charAt(j))
          flag=1;
      }
      if(flag==1)
      {
          T+=replacement;
          flag=0;
      }
      else
          T+=a;
}
return T;   
}

this should work in all versions.. i have tested this method with jdk that was available around 8 years ago.. 这应该在所有版本中都有效。.我已经在8年前使用jdk测试了此方法。

This might be what you're looking for, assuming you can use the replaceAll function you provided: 假设您可以使用提供的replaceAll函数,这可能就是您要寻找的内容:

static public String replaceAll(String str, String[] replace, String replacement )  
{        
    String replaced = str;
    for(int i=0; i<replace.length; i++)
    {
        replaced = replaceAll(str, replace[i], replacement);
    } 
    return replaced;
} 

Edit: I may have misunderstood your question, I thought you were looking for a way to replace multiple characters with a single method call, in which case you would pass the array of characters you want to replace as the second parameter in this function. 编辑:我可能误解了您的问题,我想您正在寻找一种用单个方法调用替换多个字符的方法,在这种情况下,您将要替换的字符数组作为此函数的第二个参数传递。

You can try this too. 您也可以尝试。 But I am not sure lower version than jdk 1.4 supports with toCharArray() 但是我不确定toCharArray()版本低于jdk 1.4支持的版本

 public static String replaceAll(String str, String replace, String replacement )
  {
    char[] strArray =str.toCharArray();
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<strArray.length;i++){
       if(strArray[i]==replace.toCharArray()[0]){
         sb.append(replacement);
       }else {
           sb.append(strArray[i]);
       }
    }
    return sb.toString();
 }

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

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