返回列表 發帖
  1. public class ch70
  2. {
  3.      public static void main(String args[])
  4.      {
  5.          String str1="An apple a day keeps the doctor away!";
  6.          String str2="apple";
  7.          String str3="banana";
  8.          System.out.println("字串: "+str1);
  9.          System.out.println("第一個p出現在字串裡的第"+str1.indexOf("p")+"個位置");
  10.          System.out.println("最後一個s出現在字串裡的第"+str1.indexOf("s")+"個位置");
  11.          System.out.println("自第8個位置往後找,第一個a出現在字串裡的第"+str1.indexOf("a",8)+"個位置");
  12.          System.out.println("自第8個位置往後找,最後一個a出現在字串裡的第"+str1.indexOf("a",8)+"個位置");
  13.          if(str1.indexOf(str2)<0)
  14.             System.out.println("字串裡找不到"+str2);
  15.          else
  16.             System.out.println("字串裡有"+str2+",出現在第"+str1.indexOf(str2)+"個位置");
  17.          if(str1.indexOf(str3)<0)
  18.             System.out.println("字串裡找不到"+str3);
  19.          else
  20.             System.out.println("字串裡有"+str3+",出現在第"+str1.indexOf(str3)+"個位置");
  21.      }
  22. }
複製代碼

TOP

返回列表