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