多个位置的字符串索引 [英] index of string in multiple positions
本文介绍了多个位置的字符串索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够获得某个字符串的多个索引值,例如;
I want to be able to get the multiple index values of a certain string, for example;
System.out.print("Enter an Integer:");
Scanner input = new Scanner(System.in);
String primeNumber = input.nextLine();
System.out.printf("\n%s",primeNumber.indexOf('2'));
当输入2589729872时,我得到0的结果。
when an input of 2589729872 is entered, I get a result of 0.
我想获得0,5和9.我将如何解决这个问题?
I want to obtain 0,5 and 9. How would I go about this?
推荐答案
扫描仪输入=新扫描仪(System.in);
Scanner input = new Scanner(System.in);
字符串primeNumber = input.nextLine();
String primeNumber = input.nextLine();
int index = primeNumber.indexOf('2');
int index = primeNumber.indexOf('2');
while(index> = 0){
while(index >=0) {
System.out.println(index);
index = primeNumber.indexOf('2',index+1);
}
这篇关于多个位置的字符串索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文