當前位置:編程學習大全網 - 網站源碼 - java用String中的indexOf方法,給定壹段字符串(英文段落),用戶輸入某個單詞,求

java用String中的indexOf方法,給定壹段字符串(英文段落),用戶輸入某個單詞,求

package?com;

import?java.io.*;

public?class?Test

{

public?static?void?main(String[]?args)?throws?Exception

{

String?str?=?"abcaaabckljldabcljljf";

BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));

System.out.println("請輸入要查找的單詞");

String?findStr?=?br.readLine();

int?strIndex?=?0;

int?sum?=?0;

if?(findStr?!=?null?&&?findStr.length()?!=?0)

{

strIndex?=?str.indexOf(findStr);

if?(strIndex?==?-1)

{

System.out.println("沒找到");

}

else

{

System.out.print("位置:");

while?(strIndex?!=?-1)

{

System.out.print(strIndex?+?"?");

//從之前找的位置的後面接著找

strIndex?=?str.indexOf(findStr,?strIndex?+?1);

sum++;

}

System.out.println("\r\n總***:"?+?sum?+?"個");

}

}

}

}

  • 上一篇:什麽是字幕廣告?什麽是對聯廣告?
  • 下一篇:csgo賬號被盜,郵箱也改了,怎麽辦?steam
  • copyright 2024編程學習大全網