使用Java删除字符串中的所有元音 [英] Remove all vowels in a string with Java
问题描述
我正在为我的计算机科学课程做作业.任务是获取用户输入,删除所有元音,然后打印新语句.
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)", "")
但是我的讲师希望我使用嵌套的if和else if语句来实现结果.现在我正在使用这样的东西:
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(Character.isLetter('a')){
'do something'
}else if(Character.isLetter('e')){
'do something else'
但是我不确定在if
和else if
语句中要做什么.我应该删除这封信吗?还是有更好的方法来做到这一点?
But I am not sure what to do inside the if
and else if
statements. Should I delete the letter? Or is there a better way to do this?
由于这是我的作业,我不希望完整的答案只是提示.谢谢!
Seeing as this is my homework I don't want full answers just tips. Thanks!
推荐答案
Character.isLetter('a')
Character.isLetter( char)告诉您输入的值是否是字母,在这种情况下(您已经知道"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).
您可能想使用相等运算符==
来查看您的字符是否为"a",例如:
You probably want to use the equality operator, ==
, to see if your character is an "a", like:
char c = ...
if(c == 'a') {
...
} else if (c == 'e') {
...
}
您可以通过多种方式获取字符串中的所有字符:
You can get all of the characters in a String in multiple ways:
- 作为具有
String.toCharArray() - 使用 String.charAt(index)
- As an array with String.toCharArray()
- Getting each character from the String using String.charAt(index)
这篇关于使用Java删除字符串中的所有元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!