减少大写或小写之前的重复单词

时间:2018-11-29 23:11:43

标签: java

主要

public class Main                                       
{                                       
  public static void main(String[] args)                                        
  {                                     
     System.out.println(Dupe.Eliminate("Testing UppeR and loweR"));                                                                                                         
     System.out.println(Dupe.Eliminate("UppeR is BetteR"));                                                                         

  }                                     
}

班级

public class Dupe                                       
{                                       
  public static String Eliminate(String input)                                      
  {                                     
    char[] chrArray = input.toCharArray();                                      
    String letter ="";                                      

    for (char value:chrArray){                                      
      if (letter.indexOf(value) == -1){                                     
        letter += value;                                        
      }                                     
    }                                       
    return letter;                                      
  }                                     
}    

我正在尝试消除重复的字母,例如你好是Helo。但是,我已经实现了,我想要实现的是,不管它是大写还是小写都没有关系,它仍将被归类为重复项,所以Hehe将是He,而不是Heh。我应该等于...每个字母还是有一种有效的方法?很抱歉问你们这个问题是否简单。

4 个答案:

答案 0 :(得分:1)

这就是我的处理方式。这可能不是最有效的方法,但是可以尝试一下。

  public class Main                                       
  {                                       
     public static void main(String[] args)                                        
     {                                     
        System.out.println(Dupe.Eliminate("Testing UppeR and loweR"));                                                                                                                                                                           

     }                                     
  }

  class Dupe                                       
  {                                       
     public static String Eliminate(String input)                                      
     {                                     
       char[] chrArray = input.toCharArray();                                      
       String letter ="";                                
       for(int index = 0; index < chrArray.length; index++)
       {
           int j = 0;
           boolean flag = true;
          //this while loop is used to check if the next character is already existed in the string (ignoring the uppercase or lowercase)
           while(j < letter.length())
           {
               if((int)chrArray[index] == letter.charAt(j) || (int)chrArray[index] == ((int)letter.charAt(j)+32) ) //32 is because the difference between the ascii value of the uppercase and lowercase letter is 32
               {
                   flag = false;
                   break;
               }
               else
                   j++;
           }

          if(flag == true)
          {
            letter += chrArray[index];
          }
      }

      return letter;                                      
    }                                     
  }    

答案 1 :(得分:0)

您可以使用大写和小写字符进行2个检查:

query.Parameters.AddWithValue("@email", email);

答案 2 :(得分:0)

在这里,无论序列中有多少个字符,它将替换所有重复的字符。

do i = 1, m+1
  do j = 1, m+1
    do while ( w < 2*m )
      if ( i > j ) then
        ma(i,j) = 0
      else 
        w = i-1
        ma(i, j) = w
        w = w +1 
      end if
    end do
  end do
end do

如果您要查找像public static void main(String[] args) { String duped = "aaabbccddeeffgg"; final Pattern p = Pattern.compile("(\\w)\\1+"); final Matcher m = p.matcher(duped); while (m.find()) System.out.println("Duplicate character " + (duped = duped.replaceAll(m.group(), m.group(1)))); } 这样的重复项来替换两个a,请尝试使用abacd中给出的正则表达式

答案 3 :(得分:0)

这是另一种(有状态的)方法:-

String s = "Hehe";
Set<String> found = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
String result = s.chars()
        .mapToObj(c -> "" + (char) c)
        .filter(found::add)
        .collect(Collectors.joining());
System.out.println(result);

输出:He

相关问题