2013年9月5日 星期四

String - indexOf()

String 類別 (class) 有 compareTo() 方法 (method)。
indexOf()可用來搜尋字串,並回傳字串所在的數值。


public class Test {
 public static void main(String[] args){
  String text = "To be or not to be, that is the question;"+
    " Whether 'tis nobler in the mind to suffer"+
    " the slings and arrows of outrageous fortune,"+
    " or to take arms against a sea of troubles," +
    " and by opposing end them?";
  int aIndex = -1;
  int count = 0;
  while((aIndex = text.indexOf("the", ++aIndex)) > -1){
   ++count;
  }
  System.out.println("Num of 'the' is : " + count);
 }
}

沒有留言:

張貼留言