如何删除字符串中的重复字符?

时间:2015-07-13 15:08:35

标签: java string

我有一个方法,它应该删除在其中传递的字符串中的所有重复项。我可以打印复制品,如何在原始字符串中删除它们?比如说,“迈阿密”将在它返回时需要成为“mia”。感谢。

  public static String removeDups( String str1){

        char[] str = str1.toCharArray();
        if (str == null) return null;
        int len = str.length;
        if (len < 2) return new String(str);

        char[] newStr = new char[len+1];  
        int copyLength = 0; 

        for ( int i = 1 ; i < len; i++){

            for( int j=0; j< i; j++){

                if ( str[i] == str[j]){
                    System.out.println(" str[i] == str[j] = "+ str[i] + " , "+str[j]);
                    break; 
                }

            }

        }

        return new String(str); 
    }

5 个答案:

答案 0 :(得分:2)

我发现使用StringBuilder而不是String

会更容易

修改

这也可以通过StringBuilderregex

的组合来完成

正则表达式模式分解:

 (.)    --> matches any character and puts in group 1. 
 ?=     --> this is called a positive lookahead. 
 ?=.*\\1  --> positive lookahead of zero or more characters for the first group

代码示例:

public static void main(String[] args) throws Exception {
    System.out.println(removeDuplicates("miamimiamimiami"));
    System.out.println(removeDuplicatesRegex("miamimiamimiami"));
}

public static String removeDuplicates(String input){
    StringBuilder data = new StringBuilder(input);
    for (int i = 0; i < data.length(); i++) {
        String character = String.valueOf(data.charAt(i));
        int dupIndex = data.indexOf(character, i + 1);
        while (dupIndex != -1) {
            data.deleteCharAt(dupIndex);
            dupIndex = data.indexOf(character, i + 1);
        }
    }

    return data.toString();
}

public static String removeDuplicatesRegex(String input) {

    return new StringBuilder(
            new StringBuilder(input)
                    .reverse()
                    .toString()
                    .replaceAll("(.)(?=.*\\1)", ""))
            .reverse().toString();
}

结果:

mia
mia

答案 1 :(得分:1)

这是另一种选择:

  StringBuffer buf = new StringBuffer( originalString);
  int len = str.length();
  for (int i = 0; i < len; i++) {
     char c = buf.charAt( i );
     for (int j = len - 1; j > i; j--) {
        if ( buf.charAt( j ) == c ) {
           buf.deleteCharAt( j );
           len--;
        }
     }
  }

答案 2 :(得分:1)

删除字符串中的所有重复字符:

public static String removeDuplicates(String str1) {

    if (str1 == null)
        return null;

    char[] str = str1.toCharArray();

    int len = str.length;

    if (len < 2)
        return str1;

    char temp = str[0];

    for (int i = 1; i < len; i++) {

        if (temp != 0)
            for (int j = i; j < len; j++)
                if (temp == str[j])
                    str[j] = 0;

        temp = str[i];
    }

    int i = 0;

    char[] str2 = new char[len];

    for (char c : str)
        if (c != 0)
            str2[i++] = c;

    return (new String(str2)).trim();
}

答案 3 :(得分:1)

您还可以使用 Arraylist 来存储唯一字符:

public static String removeDups( String str1){
ArrayList<Character> set=new ArrayList<Character>();
    char[] str=str1.toCharArray();
    for(int i=0;i<str.length;i++)
     {
        if(!set.contains(str[i])){
            set.add(str[i]);
        }
     }
    for(char e:set)
    {
        System.out.print(e);
    }    
}

答案 4 :(得分:0)

你的循环似乎有点奇怪。

char[] str = str1.toCharArray();
int len = str.length;
if (str == null || len  < 2) return null;

char[] newStr = new char[len+1];  
int newStrLength = 0;  

for( int i = 1; i < len; i++ ){  //Iterate complete string
    for(  int j=0; j < i; j++ ){  //Iterate the already-finished part
        if( str[i] == str[j] ) 
            break; 
        }
        newStr[newStrLength++] = str[i];
    }
}
newStr[newStrLength] = 0;

在这个例子中,我创建了一个完整的新字符串,并且不会改变原始字符串。它使代码更清晰。

或者你可以看一下这个有效性更高的question