- public class ch70
- {
- public static void main(String args[])
- {
- String str1="An apple a day keeps the docter away!";
- String str2="apple";
- String str3="banana";
- System.out.println("字串"+str1);
- System.out.println("第一個p出現在字串裡的第"+str1.indexOf("p")+"個位置");
- System.out.println("最後一個S出現在字串裡的第"+str1.lastIndexOf("s")+"個位置");
- System.out.println("自第八個位置後找,第一個A在第"+str1.indexOf("a",8)+"個位置");
- System.out.println("自第八個位置前找,最後一個A在第"+strf1.lastIndexOf("a",8)+"個位置");
- if(str1.indexOf(str2<0))
- System.out.println("字串裡找不到"+str2);
- else
- System.out.println("字串裡有"+str2+",出現在第"+str1.indexOf(str2)+"個位置");
- if(str1.indexOf(str3<0))
- System.out.println("字串裡找不到"+str3);
- else
- System.out.println("字串裡有"+str3+",出現在第"+str1.indexOf(str3)+"個位置");
- }
- }
複製代碼 |