- public class ch56
- {
- public static void main(String args[])
- {
- String str="An apple a day keeps the doctor away!";
- System.out.println("字串: "+str);
- System.out.println("第一個a出現在字串裡的第"+str.indexOf("a")+"個位置");
- System.out.println("最後一個a出現在字串裡的第"+str.lastIndexOf("a")+"個位置");
- System.out.println("針對第8個位置之後的部分搜尋, 第一個a出現在字串裡的第"+str.indexOf("a",8)+"個位置");
- System.out.println("針對第8個位置之前的部分搜尋, 最後一個a出現在字串裡的第"+str.lastIndexOf("a",8)+"個位置");
- }
- }
複製代碼 |