如何使用 Java 在给定字符串中查找辅音?


在给定字符串中寻找元音的一种方法是使用 charAt() 方法比较字符串中的每个字符与元音字母。

示例

在线演示

public class FindingConsonants {
   public static void main(String args[]) {
      String str = new String("Hi Welcome to Tutorialspoint");
      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

更新日期:2019 年 7 月 30 日

3 千次以上查看

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.