- import java.util.Scanner;
- public class TQC210
- {
- public static void main(String[] args)
- {
- boolean find,indexfind;
- System.out.println("請輸入字串");
- Scanner s=new Scanner(System.in);
- String str=s.nextLine();
- System.out.println("請輸入要搜尋的字元或字串");
- String str2=s.nextLine();
- int index=-1;
- find=false;
- indexfind=false;
- do
- {
- if((index=str.indexOf(str2,index+1))>-1)
- {
- find=true;
- if(!indexfind)
- {
- System.out.println("第幾個位置找到了");
- indexfind=true;
- }
- System.out.println(index+1);
- }
- }while(index!=-1);
- if(!find)
- System.out.println("搜尋字元不在字串中");
- }
- }
複製代碼 |