简体   繁体   中英

How to replace replace vowels with a special character in Java?

public class ReplaceVowels {

    public static void main(String args[]) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the String:");
        String str = bf.readLine();

        char[] c = new char[str.length()];
        for (int i = 0; i < str.length(); i++) {

            if (c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o'
                    || c[i] == 'u') {

                System.out.println(str.replace(c[i], '?'));

            }

        }

    }
}

why does the str.replace method not work? What should I do to make it work?

In your code, you're creating a new array of characters, which is the same length as your string, but you're not initialising the array with any values.

Instead, try:

char[] c = str.toCharArray();

However, this isn't the best way of doing what you're trying to do. You don't need a character array or an if statement to replace characters in a string:

String str = bf.readLine();
str.replace( 'a', '?' );
str.replace( 'e', '?' );
str.replace( 'i', '?' );
str.replace( 'o', '?' );
str.replace( 'u', '?' );
System.out.println( str );

The replace function will replace any (and all) characters it finds, or it will do nothing if that character doesn't exist in the string.

You might also want to look into using regular expressions (as pointed out in edwga's answer), so that you can shorten those 5 function calls into one:

str.replaceAll( "[aeiou]", "?" );

Honestly, this solution is relatively impractical. You should use the str.replaceAll() method instead.

(read in the String str);
str = str.replaceAll("[aeiou]", "?");
System.out.println(str);

What this does is it uses the regular expression "[aeiou]" and replaces it with a special character ("?"). Regex is a complicated topic, but this one just detects every instance of a vowel. You can read more about Regex at http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html

Here is one way to replace all vowels in a string with an "X". The (?i) is to make it case insensitive.

String str = "hEllo";
str = str.replaceAll( "(?i)[aeiou]", "X" );

Could also be more explicit with case like:

String str = "hEllo";
str = str.replaceAll( "[aeiouAEIOU]", "X" );

All the above answers works. Just adding some case sensitivity to capture vowels in upper case(Using Scanner Class)

       String str1, str2;
       Scanner scan = new Scanner(System.in);

       System.out.print("Enter a String : ");
       str1 = scan.nextLine();
       str2 = str1.replaceAll("[aeiouAEIOU]", "?");
       // adding AEIOU to capture Vowels in uppercase.
       System.out.println("All Vowels Removed Successfully");

       System.out.println(str2);
/***
 * Replace all vowels in an input string with
 * the corresponding numbers replace with 1,2,3,4,5
 * @author Kishore Diyyana
 */
public class ReplaceVowels {
    public static void main(String args[]) {
        ReplaceVowels rv = new ReplaceVowels();
        System.out.println(rv.replaceVowels("Kishore Babu Diyyana"));
    }
    public String replaceVowels(String inputStr) {
        return inputStr.replaceAll("[aA]","1").
                        replaceAll("[eE]","2").
                        replaceAll("[iI]","3").
                        replaceAll("[oO]","4").
                        replaceAll("[uU]","5");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM