编写一种方法来删除 Java 字符串中的元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19329307/
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-08-12 16:10:05  来源:igfitidea点击:

Writing a method to remove vowels in a Java String

java

提问by user2864813

I am a beginner of programming, and am writing a Java method to remove vowel in Strings, but I do not know how to fix this error: ";" expected:

我是编程初学者,正在编写 Java 方法来删​​除字符串中的元音,但我不知道如何解决此错误";" expected::

public String disemvowel(String s) {

    boolean isVowel(char c);
    if (c == 'a') {
        return true;
    } else if if (c == 'e') {
        return true;
    } else if if (c == 'i') {
        return true;
    } else if if (c == 'o') {
        return true;
    } else if if (c == 'u') {
        return true;
    }
    String notVowel = "";
    int l = s.length();
    for (int z = 0; z <= l; z++) {


        if (isVowel == "false") {
            char x = s.charAt(z);
            notVowel = notVowel + x;
        }
    }
    return notVowel;
}

回答by cmd

A much simpler approach would be to do the following:

一个更简单的方法是执行以下操作:

String string = "A really COOL string";
string = string.replaceAll("[AaEeIiOoUu]", "");
System.out.println(string);

This will apply the regular expression, [AaEeIiOoUu]to string. This expression will match all vowels in the character group [AaEeIiOoUu]and replace them with ""empty string.

这会将正则表达式[AaEeIiOoUu]应用于string. 此表达式将匹配字符组中的所有元音[AaEeIiOoUu]并将它们替换为""空字符串。

回答by Vallabh Patade

 String str= "Your String";
 str= str.replaceAll("[AEIOUaeiou]", "");
 System.out.println(str);

回答by Makoto

You've got a lotof syntax errors.

你有很多语法错误。

  • boolean isVowel(char c);- not sure what you're doing with this. if you want it as a separate method, separate it out (and don't place a semicolon after it, which would be invalid syntax.

  • else if ifis invalid syntax. If you're doing an else if, then you only need the one if.

  • Even if the code would compile, for (int z = 0; z <= l; z++)will cause you to step off of the String. Remove the <=in favor of <.
  • isVowel == "false"is never going to work. You're comparing a String to a boolean. You want !isVowelinstead.
  • boolean isVowel(char c);- 不确定你在做什么。如果你想把它作为一个单独的方法,把它分开(不要在它后面放分号,这将是无效的语法。

  • else if if是无效的语法。如果你正在做一个else if,那么你只需要一个if

  • 即使代码会编译,for (int z = 0; z <= l; z++)也会导致您离开字符串。删除<=支持<.
  • isVowel == "false"永远不会工作。您正在将字符串与布尔值进行比较。你想要!isVowel

Putting the syntax errors aside, think of it like this.

抛开语法错误,这样想。

  • You have a string that contains vowels. You wish to have a string that doesn't contain vowels.
  • The most straightforward approach is to iterate over the String, placing all non-vowel characters into a separate String, which you then return.
  • 您有一个包含元音的字符串。您希望有一个不包含元音的字符串。
  • 最直接的方法是遍历字符串,将所有非元音字符放入一个单独的字符串中,然后返回该字符串。

Interestingly enough, the half-method you have there can accomplish the logic of determining whether something is or isn't a vowel. Extract that to its own method. Then, call itin your other method. Do take into account capital letters though.

有趣的是,您在那里使用的半方法可以完成确定某事物是否为元音的逻辑。将其提取到它自己的方法中。然后,在您的其他方法中调用它。不过要考虑大写字母。

I leave the rest as an exercise to the reader.

我把剩下的留给读者作为练习。

回答by Josh M

You could try something like this:

你可以尝试这样的事情:

public static String removeVowels(final String string){
    final String vowels = "AaEeIiOoUu";
    final StringBuilder builder = new StringBuilder();
    for(final char c : string.toCharArray())
        if(vowels.indexOf(c) < 0)
            builder.append(c);
    return builder.toString();
}

回答by Hot Licks

Here is your code, without changing any logic, but unscrambling the isVowel method:

这是您的代码,没有更改任何逻辑,但对 isVowel 方法进行了解读:

public String disemvowel(String s) {

    // Removed the "isVowel" method from here and moved it below

    String notVowel = "";
    int l = s.length();
    for (int z = 0; z <= l; z++) {
        // Note that the "isVowel" method has not been called.
        // And note that, when called, isVowel returns a boolean, not a String.
        // (And note that, as a general rule, you should not compare strings with "==".)
        // So this area needs a lot of work, but we'll start with this
        boolean itIsAVowel = isVowel(s.charAt(z));
        // (I made the variable name "itIsAVowel" to emphasize that it's name has nothing to do with the method name.
        // You can make it "isVowel" -- the same as the method -- but that does not in any way change the function.)
        // Now take it from there...
        if (isVowel == "false") {
            char x = s.charAt(z);
            notVowel = notVowel + x;
        }
    }
    return notVowel;
}

// You had this line ending with ";"
boolean isVowel(char c) {
    if (c == 'a') {
        return true;
   // Note that you coded "if if" on the lines below -- there should be only one "if" per line, not two        
    } else if if (c == 'e') {
        return true;
    } else if if (c == 'i') {
        return true;
    } else if if (c == 'o') {
        return true;
    } else if if (c == 'u') {
        return true;
    }
    // You were missing this final return
    return false;
}

(Yes, I know this should be a comment, but you can't put formatted code in a comment.)

(是的,我知道这应该是注释,但是您不能将格式化的代码放在注释中。)