Board logo

標題: indexOf() 與 lastIndexOf() 函式 [打印本頁]

作者: tonyh    時間: 2013-7-27 16:11     標題: indexOf() 與 lastIndexOf() 函式

本帖最後由 tonyh 於 2013-7-27 16:38 編輯
  1. //字元或字串位置, indexOf() 與 lastIndexOf() 函式
  2. public class ch70
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         String str1="An apple a day keeps the doctor away!";
  7.         String str2="apple";
  8.         String str3="banana";
  9.         System.out.println("字串: "+str1);
  10.         System.out.println("第一個p出現在字串裡的第"+str1.indexOf("p")+"個位置");
  11.         System.out.println("最後一個s出現在字串裡的第"+str1.lastIndexOf("s")+"個位置");
  12.         System.out.println("自第8個位置往後找, 第一個a出現在字串裡的第"+str1.indexOf("a",8)+"個位置");
  13.         System.out.println("自第8個位置往後找, 最後一個a出現在字串裡的第"+str1.lastIndexOf("a",8)+"個位置");
  14.         if(str1.indexOf(str2)<0)     //若找不到, indexOf()函式的回傳值為-1
  15.             System.out.println("字串裡找不到"+str2);
  16.         else
  17.             System.out.println("字串裡有"+str2+", 出現在第"+str1.indexOf(str2)+"個位置");
  18.         if(str1.indexOf(str3)<0)
  19.             System.out.println("字串裡找不到"+str3);
  20.         else
  21.             System.out.println("字串裡有"+str3+", 出現在第"+str1.indexOf(str3)+"個位置");
  22.     }
  23. }
複製代碼

作者: t2364705    時間: 2013-7-27 16:48

  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("第一個字串裡的第"+str1.indexOf("p")+"個位置");
  10.         System.out.println("最後一個字串裡的第"+str1.lastIndexOf("s")+"個位置");
  11.         System.out.println("自第8個位置往後找第一個a出現在字串裡的第"+str1.lastIndexOf("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. }
複製代碼

作者: 黃博鴻    時間: 2013-7-27 16:52

  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. }
複製代碼

作者: 尤泓鈞    時間: 2013-7-27 16:58

  1. public class ch70
  2. {
  3.   public static void main(String args[])
  4.   {
  5.     Stirng 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.indoxOf("p")+"個位置");
  10.     System.out.println("自第8個位置往後找,第一個a出現在字串裡的第"+str1.indexOf("a"+8)+"個位置");
  11.     System.out.println("自第8個位置往後找,最後一個a出現在字串裡的第"+str1.lastIndexOf("a"+8)+"個位置");
  12.     if(str1.indoxOf(str2)<0)
  13.     System.out.println("字串裡找不到"+str2);
  14.     else
  15.     System.out.println("字串裡有"+str2+"出現在第"+str1.indexOf(str2)+"個位子");
  16.     if(str1.indexOf(str3)<0)
  17.     System.out.println("字串裡找不到"+str3);
  18.     else
  19.     System.out.println("字串裡有"+str3+"出現在第"+str1.indexOf(str3)+"個位子");
  20.   }
  21. }
複製代碼

作者: t3742238    時間: 2013-7-27 16:59

  1. public class ch70
  2. {
  3.   public static void main(String args[])
  4.   {
  5.     String str1="An apple a day keeps the docter 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.lastIndexOf("s")+"個位置");
  11.     System.out.println("自第八個位置後找,第一個A在第"+str1.indexOf("a",8)+"個位置");
  12.     System.out.println("自第八個位置前找,最後一個A在第"+strf1.lastIndexOf("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. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2