java提取前两位_如何使用Java从字符串中提取前n个字符?

要在给定的String中找到辅音,请使用charAt()方法将其中的每个字符与元音字母进行比较,其余为辅音。

示例

public class FindingConsonants {

public static void main(String args[]) {

String str = new String("Hi Welcome to Nhooo");

for(int i=0; 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 H at the index 0

Given string contains W at the index 3

Given string contains l at the index 5

Given string contains c at the index 6

Given string contains m at the index 8

Given string contains t at the index 11

Given string contains T at the index 14

Given string contains t at the index 16

Given string contains r at the index 18

Given string contains l at the index 21

Given string contains s at the index 22

Given string contains p at the index 23

Given string contains n at the index 26

Given string contains t at the index 27

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值