在Java中如何使用indexOf计数?
我想替换字符串并打印它被替换了多少次在Java中如何使用indexOf计数?,java,for-loop,if-statement,indexof,Java,For Loop,If Statement,Indexof,我想替换字符串并打印它被替换了多少次 for (int i = 0; i < input.length(); i++) { if (inputL.indexOf(curStrL, i) > -1) { cnt++; i = (inputL.indexOf(curStrL, i)); // EDIT by Shraft i = i + curStr.length()
for (int i = 0; i < input.length(); i++) {
if (inputL.indexOf(curStrL, i) > -1) {
cnt++;
i = (inputL.indexOf(curStrL, i)); // EDIT by Shraft
i = i + curStr.length() - 1; // EDIT
// *** to make the code find from the next letter! ***
} else
continue;
} // for
例如)输入:aabba
发件人:aa
致:bb
ddbba
替换:1 输入:AAccaabbaaaaatt
发件人:aa
致:bb
ddccddbbddatt
替换:4 我这里有个问题:
for (int i = 0; i < input.length(); i++) {
if (inputL.indexOf(curStrL, i) > -1) {
cnt++;
i = (inputL.indexOf(curStrL, i))+1; // this part!
} else
continue;
} // for
for(int i=0;i-1){
cnt++;
i=(inputL.indexOf(curStrL,i))+1;//这部分!
}否则
继续;
}//为了
我的老师说只要使用.indexOf和。替换,和。toLowerCase
她举了一些例子,它们总是把两个字母替换成两个字母。
这就是我用“+1”来查找另一个字母的原因。
如果我去掉“+1”,它会计算两次“aaa”。(aa a和a aa。它被替换为“dda”,所以它是错误的。)
但这一次,当我只替换一个字母(例如a)时,它的数字比实际数字要少。(例如,“aaa”只计算两次。)
有了老师的例子,效果很好,因为它们都替换了两个字母。
但我想改进这个
以下是我的全部代码:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Input : ");
String input = scan.next();
System.out.print("from : ");
String curStr = scan.next();
System.out.print("to : ");
String chStr = scan.next();
String inputL = input.toLowerCase();
String curStrL = curStr.toLowerCase();
String chStrL = chStr.toLowerCase();
String output = inputL.replace(curStrL, chStrL);
int cnt = 0;
if (inputL.indexOf(curStrL) == -1) {
System.out.println("Do it again");
} else
System.out.println(output);
for (int i = 0; i < input.length(); i++) {
if (inputL.indexOf(curStrL, i) > -1) {
cnt++;
i = (inputL.indexOf(curStrL, i))+1;
// *** to make the code find from the next letter! ***
} else
continue;
} // for
if (cnt > 0)
System.out.println("replaced : " + cnt);
else
{System.out.println("can't replace. Do it again");
break;}
System.out.println("----------------");
} // while
} // main
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
while(true){
系统输出打印(“输入:”);
字符串输入=scan.next();
系统输出打印(“从:”);
字符串curStr=scan.next();
系统输出打印(“至:”);
字符串chStr=scan.next();
字符串inputL=input.toLowerCase();
字符串curStrL=curStr.toLowerCase();
字符串chStrL=chStr.toLowerCase();
字符串输出=inputL.replace(curStrL,chStrL);
int-cnt=0;
if(inputL.indexOf(curStrL)=-1){
System.out.println(“再做一次”);
}否则
系统输出打印项次(输出);
对于(int i=0;i-1){
cnt++;
i=(inputL.indexOf(curStrL,i))+1;
//***使代码从下一个字母中找到***
}否则
继续;
}//为了
如果(cnt>0)
System.out.println(“替换:“+cnt”);
其他的
{System.out.println(“无法替换。请再次执行”);
中断;}
系统输出打印项次(“------------------------------”);
}//而
}//主要
只需使用要替换的字符串长度增加循环的计数器变量即可
for (int i = 0; i < input.length(); i++) {
if (inputL.indexOf(curStrL, i) > -1) {
cnt++;
i = (inputL.indexOf(curStrL, i)); // EDIT by Shraft
i = i + curStr.length() - 1; // EDIT
// *** to make the code find from the next letter! ***
} else
continue;
} // for
for(int i=0;i-1){
cnt++;
i=(inputL.indexOf(curStrL,i));//按Shraft编辑
i=i+curStr.length()-1;//编辑
//***使代码从下一个字母中找到***
}否则
继续;
}//为了