试图从字符串中删除字符

时间:2016-11-15 08:23:22

标签: java

我正在尝试从随机生成的20个字母单词中删除元音。我必须使用子字符串进行此分配,但在概念上遇到问题。使用此代码,单词的值永远不会更改并保持其原始值。当我为字符串的后半部分执行i + 2时,它跳过两个字符并且当有两个元音直接相继时会混乱。如果我必须使用子字符串,我怎样才能每次都改进它。

public class WordsWithoutVowels {
    String finalWord;

    public WordsWithoutVowels(String word) {
        for(int i = 0; i < 20; i++) {
            if(word.charAt(i) == 'a') {word = word.substring(0, i) + word.substring(i++);} 
            if(word.charAt(i) == 'e') {word = word.substring(0, i) + word.substring(i++);} 
            if(word.charAt(i) == 'i') {word = word.substring(0, i) + word.substring(i++);} 
            if(word.charAt(i) == 'o') {word = word.substring(0, i) + word.substring(i++);} 
            if(word.charAt(i) == 'u') {word = word.substring(0, i) + word.substring(i++);} 
            if(word.charAt(i) == 'y') {word = word.substring(0, i) + word.substring(i++);}        
            System.out.println(word);
        }
        finalWord = word;
    }

    public String getWord()
    {
        return finalWord;
    }
}

8 个答案:

答案 0 :(得分:2)

您可以使用以下简化代码:

<DataGrid ItemsSource="{Binding Path=JobCollectionView}" [..]>
    <DataGrid.GroupStyle>
        <GroupStyle>
           <GroupStyle.HeaderTemplate>
               <DataTemplate>
                  <TextBlock Text="{Binding Path=State}" />
               </DataTemplate>
            </GroupStyle.HeaderTemplate>                   
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                   <Setter Property="Template">
                      <Setter.Value>
                         <ControlTemplate TargetType="{x:Type GroupItem}">
                             <Expander IsExpanded="True">
                                  <Expander.Header>
                                     <StackPanel Orientation="Horizontal">
                    //Error is here:     <TextBlock Text="{Binding Path=StateDescription}" />
                                         <TextBlock Text="{Binding Path=ItemCount}"/>
                                         <TextBlock Text=" Jobs"/>
                                                </StackPanel>
                                       </Expander.Header>
                                     <ItemsPresenter />
                                  </Expander>
                               </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                     </Style>
             </GroupStyle.ContainerStyle>
     </GroupStyle>
 </DataGrid.GroupStyle>

答案 1 :(得分:0)

为什么不使用简单的正则表达式?

String str = "Hello world!";
str = str.replaceAll("[AEIOUaeiou]", "");

str的新值将为Hll wrld!

答案 2 :(得分:0)

我修复了原始解决方案。首先 - 迭代每个字母 - 到word.length()。其他问题是增加“我”。

for (int i = 0; i < word.length(); ) {
  if (word.charAt(i) == 'a') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  if (word.charAt(i) == 'e') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  if (word.charAt(i) == 'i') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  if (word.charAt(i) == 'o') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  if (word.charAt(i) == 'u') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  if (word.charAt(i) == 'y') {
    word = word.substring(0, i) + word.substring(i+1);
    continue;
  }
  i++;
  System.out.println(word);
}
finalWord = word;

这里更优雅的方式:

public class WordsWithoutVowels {

  String finalWord;
  List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u', 'y');


  public WordsWithoutVowels(String word) {
    for (int i = 0; i < word.length(); ) {
      if (vowels.contains(word.charAt(i))) {
        word = word.substring(0, i) + word.substring(i + 1);
        continue;
      }
      i++;
      System.out.println(word);
    }
    finalWord = word;
  }


  public String getWord() {
    return finalWord;
  }
}

答案 3 :(得分:0)

问题出在您的i++运营商身上。它会扰乱你的角色检查以及你的循环。后递减运算符(i++)将i的原始值作为方法参数,但对于所有后续指令将其值增加1.

为了说明这一点,我们假设输入是&#34; Treyarch&#34;。然后,在索引2处,i处的字符为e。和指示 word = word.substring(0, i) + word.substring(i++);会产生以下后果:

word = Treyarch

i = 3

它不仅不会从String驱逐元音,还会弄乱你的索引(i)。因为对于循环的其余部分,i为4,当您进入下一次迭代时,它将为5,因此您的程序永远不会检查前两个if条件。

正如所解释的,此解决方案的最佳实践是检查一个if语句中的任何条件并使用不可变索引。换句话说:

for(int i = 0; i < word.length(); i++) {
    if(word.charAt(i) == 'a' || word.charAt(i) == 'e' || word.charAt(i) == 'i' || word.charAt(i) == 'o' || word.charAt(i) == 'u' || word.charAt(i) == 'y') {
        word = word.substring(0, i) + word.substring(i + 1);
}

答案 4 :(得分:0)

您的增量逻辑出现问题。删除字符时,您错过了以下字符。

如果使用switch语句,您的代码可能更容易阅读。

  public class WordsWithoutVowels {
        String finalWord;

        public WordsWithoutVowels(String word) {
            // don't go too far !!
            for (int i = 0; i < word.length(); i++) {
                switch (word.charAt(i)) {
                    case 'a':
                    case 'e':
                    case 'i':
                    case 'o':
                    case 'u':
                    case 'y':
                        // remove this vowel
                        word = word.substring(0, i) + word.substring(i + 1);
                        // step back in order to analyse the new i-th char
                        i--;

                }
                System.out.println(word);
            }
            finalWord = word;
        }

        public String getWord() {
            return finalWord;
        }
    }

答案 5 :(得分:0)

public static void main(String[] args) 
{
 String WordsWithoutVowels ="mayank";
 String updated="";
 for(char char1: WordsWithoutVowels.toCharArray())
 {
 switch(char1)
 {
     case 'a':
     case 'e':
     case 'i':
     case 'o':
     case 'u':
         break;
     default:
         updated+=char1;

 }
 }
    System.out.println(updated);
}

答案 6 :(得分:0)

简化您的解决方案以满足要求。

public class WordsWithoutVowels {
    String finalWord;

    public WordsWithoutVowels(String word) {
        StringBuilder sb = new StringBuilder(word);

        for(int i = 0; i < sb.length;) {
            if(sb.charAt(i)=='a' || sb.charAt(i)=='e' || sb.charAt(i)=='i' || sb.charAt(i)=='o' || sb.charAt(i)=='u')
                sb.deleteCharAt(i);
            else
                i++;
            }
        finalWord = sb.toString();
    }

    public String getWord()
    {
        return finalWord;
    }
}

答案 7 :(得分:0)

   public WordsWithoutVowels(String word) {         
     for (int i = 0; i < 20; i++) 
          {
    if (word.charAt(i) == 'a') {
        word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             } else if (word.charAt(i) == 'e') {
                word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             } else if (word.charAt(i) == 'i') {
                word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             } else if (word.charAt(i) == 'o') {
                word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             } else if (word.charAt(i) == 'u') {
                word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             } if (word.charAt(i) == 'y') {
                word = word.substring(0, i) + (i == 19 ? "" : word.substring(++i));             }           System.out.println(word + " " + i);
        }       finalWord = word;   }