如何从Java中的单词中删除重复的字母

时间:2015-06-12 18:50:55

标签: java string ascii

我在编写java代码以删除word中重复的字母时遇到问题。此代码将通过仅接受重复的一个字母来删除重复的字母。假设,如果输入是" SUSHIL"然后输出将是" SUHIL"。 我写这个java代码。

SELECT DISTINCT waits.ride, 
                T2.daywait, 
                T3.timewait, 
                T4.daytimewait, 
                T5.currentwait 
FROM   waits 
       left join (SELECT Avg(waittime) AS dayWait, 
                         ride 
                  FROM   waits 
                  WHERE  dayofweek = '" . $dow . "' 
                  GROUP  BY ride) AS T2 
              ON T2.ride = waits.ride 
       left join (SELECT Avg(waittime) AS timeWait, 
                         ride 
                  FROM   waits 
                  WHERE  currenttime >= '" . $minusTime . "' 
                         AND currenttime <= '" . $plusTime . "' 
                  GROUP  BY ride) AS T3 
              ON T3.ride = waits.ride 
       left join (SELECT Avg(waittime) AS dayTimeWait, 
                         ride 
                  FROM   waits 
                  WHERE  currenttime >= '" . $minusTime . "' 
                         AND currenttime <= '" . $plusTime . "' 
                         AND dayofweek = '" . $dow . "' 
                  GROUP  BY ride) AS T4 
              ON T4.ride = waits.ride 
       left join (SELECT waittime AS currentWait, 
                         ride 
                  FROM   waits 
                  GROUP  BY ride 
                  ORDER  BY wid DESC) AS T5 
              ON T5.ride = waits.ride 
WHERE  park = '" . getPark() . "'

7 个答案:

答案 0 :(得分:2)

试试这个:

    private static String removeRepeat(String input){

    Set<Character> str = new LinkedHashSet<Character>();

    for(int n=0;n<input.length();n++){

        str.add(input.charAt(n));

    }

    return str.toString();

}

评论中的好点,改为LinkedHashSet。

它可能是废话代码,但我的意思是不要重新发明轮子,只有你必须

答案 1 :(得分:1)

    char ch1,ch2;
    int l=name.length();
    String result="";
    for(int i=0;i<l;i++){ 
        if(name.indexOf(name.charAt(i))==i){
             result+=name.charAt(i);
        }
    }
    System.out.println(result);

输入= SUSHSILHI
output = SUHIL

答案 2 :(得分:0)

你应该这样做:将第一个字母添加到结果中,然后检查下一个字母是否已经存在于结果中:

boolean exist=false;
result=name.charAt(0);
for (i=1; i<l;i++) {
    exist=false;
    int j=0;
    while (exist=false && j<i) {
        if(name.charAt(i)==charAt(j)) {
             exist=true;
        }
        j++;
    }
    if(exist==false){
         result=result+name.charAt(i);
    }
}

for检查所有字符串名称,然后while检查结果中已存在的字符,如果它尚不存在,则不执行任何操作。

答案 3 :(得分:0)

使用#! /bin/bash forever start ../app1/app.js forever start ../app2/app.js ,一个for循环应该可以工作,如下所示

indexOf()

答案 4 :(得分:0)

        String name = "SUSHIL";
        char ch1 = 0, ch2;
        int i, j;
        int l = name.length();
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < l; i++)
        {
            //this is used to append char to StringBuilder
            boolean shouldAppend = true;
            //if we don't check if the length is equal to 0 to start then the below loop will never run and the result would be an empty string so just append the first character to the StringBuilder
            if (sb.length() == 0)
            {
                sb.append(name.charAt(i));
                shouldAppend = false;
            }
            else
            {
                for (j = 0; j < sb.length(); j++)
                {
                    ch1 = name.charAt(i);
                    ch2 = sb.charAt(j);
                    if (ch1 == ch2)
                    {
                        //StringBuilder contains ch1 so turn shouldAppend to false and break out of this inner loop
                        shouldAppend = false;
                        break;
                    }
                }
            }
            if (shouldAppend) sb.append(ch1);

        }
        System.out.println("Output:" + sb.toString());

答案 5 :(得分:0)

尝试:

//Globally
List<Character> list = new ArrayList<Character>();    

public String remRepeats(String original)
{
   char ch = original.charAt(0);        

    if (original.length() == 1)
        return original;

    if (list.contains(ch))
        return remRepeats(original.substring(1));
    else
    {
        list.add(ch);
        return ch + remRepeats(original.substring(1));
    }
}

答案 6 :(得分:0)

List<Character> characters = new ArrayList<>();
char[] chars = name.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for(char currChar:chars) {
   if (!characters.contains(currChar)) {
         characters.add(currChar);
         stringBuilder.append(currChar);
        }
    }
System.out.println(stringBuilder);