- public class ch70
- {
- public static void main(String args[])
- {
- String str1="An apple a day keeps the doctor away!";
- String str2="apple";
- String str3="banana";
- System.out.println("字串"+str1);
- System.out.println("第一個字串裡的第"+str1.indexOf("p")+"個位置");
- System.out.println("最後一個字串裡的第"+str1.lastIndexOf("s")+"個位置");
- System.out.println("自第8個位置往後找第一個a出現在字串裡的第"+str1.lastIndexOf("a",8)+"個位置");
- System.out.println("自第8個位置往後找最後一個a出現在字串裡的第"+str1.indexOf("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)+"個位置");
- }
- }
複製代碼 |