如何使用Java在给定的字符串中查找辅音?
在给定的字符串中找到元音的一种方法是使用charAt()方法将每个字符与元音字母进行比较。
示例
public class FindingConsonants {
public static void main(String args[]) {
String str = new String("Hi Welcome to Nhooo");
for(int i=0; i<str.length(); i++) {
if(str.charAt(i) == 'a'|| str.charAt(i) == 'e'|| str.charAt(i) == 'i' ||
str.charAt(i) == 'o' || str.charAt(i) == 'u'|| str.charAt(i) == ' ') {
} else {
System.out.println("Given string contains "+str.charAt(i)+" at the index "+i);
}
}
}
}输出结果
Given string contains i at the index 1 Given string contains e at the index 4 Given string contains o at the index 7 Given string contains e at the index 9 Given string contains o at the index 12 Given string contains u at the index 15 Given string contains o at the index 17 Given string contains i at the index 19 Given string contains a at the index 20 Given string contains o at the index 24 Given string contains i at the index 25
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短