【发布时间】:2018-01-09 21:09:37
【问题描述】:
我这里有这段代码,它读取一个字符串和一个字符,然后在该字符串中找到该字符的所有出现。但是,它只找到特定的大小写,我如何让它检测大小写?
String str = oIn.nextLine();
char ch = oIn.nextLine().charAt(0);
int count = 0;
int index = str.indexOf(ch);
while (index >= 0){
index = str.indexOf(ch, index+1);
count++;
System.out.println ("The amount of the character " + ch + " in " + str +" are " +count);
}
【问题讨论】:
-
String str = oIn.nextLine().toUpperCase();或String str = oIn.nextLine().toLowerCase(); -
用
Character.toLowerCase将字符转为小写并比较或使用String::toLowerCase比较字符串。
标签: java string while-loop char indexof