java 用Java删除字符串中的所有元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13167495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:44:17  来源:igfitidea点击:

Remove all vowels in a string with Java

javajava.util.scanner

提问by Chris Frank

I am doing a homework assignment for my Computer Science course. The task is to get a users input, remove all of the vowels, and then print the new statement.

我正在为我的计算机科学课程做家庭作业。任务是获取用户输入,删除所有元音,然后打印新语句。

I know I could easily do it with this code:

我知道我可以使用以下代码轻松完成:

string.replaceAll("[aeiou](?!\b)", "")

But my instructor wants me to use nested if and else if statements to achieve the result. Right now I am using something like this:

但是我的导师希望我使用嵌套的 if 和 else if 语句来实现结果。现在我正在使用这样的东西:

if(Character.isLetter('a')){
    'do something'
}else if(Character.isLetter('e')){
    'do something else'

But I am not sure what to do inside the ifand else ifstatements. Should I delete the letter? Or is there a better way to do this?

但我不确定在ifandelse if语句中做什么。我应该删除这封信吗?或者有没有更好的方法来做到这一点?

Seeing as this is my homework I don't want full answers just tips. Thanks!

既然这是我的作业,我不想要完整的答案只是提示。谢谢!

回答by arshajii

I think what he might want is for you to read the string, create a new empty string (call it s), loop over your input and add all the characters that are not vowels to s(this requires an ifstatement). Then, you would simply print the contents of s.

我认为他可能想要的是让您读取字符串,创建一个新的空字符串(称之为s),遍历您的输入并将所有不是元音的字符添加到s(这需要一个if语句)。然后,您只需打印s.



Edit:You might want to consider using a StringBuilderfor this because repetitive string concatenation can hinder performance, but the idea is the same. But to be honest, I doubt it would make a noticeable difference for this type of thing.

编辑:您可能需要考虑StringBuilder为此使用 a ,因为重复的字符串连接会影响性能,但想法是相同的。但老实说,我怀疑这会对这类事情产生显着的影响。

回答by Brendan Long

Character.isLetter('a')

Character.isLetter(char)tells you if the value you give it is a letter, which isn't helpful in this case (you already know that "a" is a letter).

Character.isLetter(char)告诉你你给它的值是否是一个字母,这在这种情况下没有帮助(你已经知道“a”是一个字母)。

You probably want to use the equality operator, ==, to see if your character is an "a", like:

您可能想使用相等运算符 ,==来查看您的字符是否为“a”,例如:

char c = ...
if(c == 'a') {
    ...
} else if (c == 'e') {
    ...
}

You can get all of the characters in a String in multiple ways:

您可以通过多种方式获取 String 中的所有字符:

回答by rmoh21

If you want to do it in O(n) time

如果你想在 O(n) 时间内完成

  • Iterate over the character array of your String
  • If you hit a vowel skip the index and copy over the next non vowel character to the vowel position.
  • You will need two counters, one which iterates over the full string, the other which keeps track of the last vowel position.
  • After you reach the end of the array, look at the vowel tracker counter - is it sitting on a vowel, if not then the new String can be build from index 0 to 'vowelCounter-1'.
  • 遍历字符串的字符数组
  • 如果您击中元音,则跳过索引并将下一个非元音字符复制到元音位置。
  • 您将需要两个计数器,一个迭代整个字符串,另一个跟踪最后一个元音位置。
  • 到达数组末尾后,查看元音跟踪器计数器 - 它是否位于元音上,如果不是,则可以从索引 0 到 'vowelCounter-1' 构建新字符串。

If you do this is in Java you will need extra space to build the new String etc. If you do it in C you can simply terminate the String with a null character and complete the program without any extra space.

如果你在 Java 中这样做,你将需要额外的空间来构建新的 String 等。如果你在 C 中这样做,你可以简单地用空字符终止 String 并在没有任何额外空间的情况下完成程序。

回答by Yogendra Singh

I think you can iterate through the character check if that is vowel or not as below:

我认为您可以遍历字符检查是否为元音,如下所示:

  define a new string 
  for(each character in input string)
    //("aeiou".indexOf(character) <0) id one way to check if character is consonant
    if "aeiou" doesn't contain the character  
      append the character in the new string

回答by dasblinkenlight

I don't think your instructor wanted you to call Character.isLetter('a')because it's always true.

我不认为你的教练想让你打电话,Character.isLetter('a')因为它总是true

The simplest way of building the result without regexp is using a StringBuilderand a switchstatement, like this:

不使用正则表达式构建结果的最简单方法是使用 aStringBuilder和 aswitch语句,如下所示:

String s = "quick brown fox jumps over the lazy dog";
StringBuffer res = new StringBuffer();
for (char c : s.toCharArray()) {
    switch(c) {
        case 'a': // Fall through
        case 'u': // Fall through
        case 'o': // Fall through
        case 'i': // Fall through
        case 'e': break; // Do nothing
        default: // Do something
    }
}
s = res.toString();
System.out.println(s);

You can also replace this with an equivalent if, like this:

您也可以将其替换为等效的if,如下所示:

if (c!='a' && c!='u' && c!='o' && c!='i' && c!='e') {
    // Do something
}