1. 程式人生 > >String類的indexOf()方法

String類的indexOf()方法

String類的indexOf()方法

1.程式碼實戰

    public static void main(String[] args) {      
      //輸出args的值
      System.out.print("args are :");
      for(int i = 0;i< args.length;i++)
      {
          String arg = args[i];
          System.out.println(args[i]);
          int equal = arg.indexOf('=', 5); //從第5個字元開始往後找出第一個字元為=的下標【但是下標仍然是從0開始計數】
System.out.println("equal is :"+equal); equal = arg.indexOf('=', 0);// 從頭開始找,返回第一個字元為=的下標 System.out.println("equal is :"+equal); } }

輸出結果是:

args are :--zk=quorum=192.168.211.4
equal is :11
equal is :4

重點內容見程式碼註釋。 看到這裡,可能有很多人自以為對indexOf()方法以及理解很深了,但實際上真的是這樣麼?檢視下面這段程式碼:

public static void test6(){
        String name = "Lawson";
        int j = name.indexOf(97, 0);
        System.out.println("j = "+j);
    }

輸出結果如下:

j = 1

這裡就有點疑問了,為啥寫作indexOf(97,0)都是可以的?難道不應該寫成indexOf(a,0)的嗎? 帶著疑問,開啟jdk原始碼:

2.indexOf()原始碼

/**
     Returns the index within this string of the first occurrence of the
     specified character, starting the search at the specified index.
	 返回這個字串從指定的下標開始,指定字元在這個字串中第一次出現的索引。
	      
     If a character with value ch occurs in the
     character sequence represented by this String
     object at an index no smaller than fromIndex, then
     the index of the first such occurrence is returned. 
	 如果一個字元,其值為ch,出現在字串序列由這個字串物件,其下標如果不小於fromIndex,
	 那麼第一次出現該字元的下標索引會被返回
	 
	 For values of ch in the range from 0 to 0xFFFF (inclusive),
     this is the smallest value k such that:     
     (this.charAt(k) == ch) && (k <= fromIndex)  is true. 
	 
	 For other values of ch, it is the smallest value k such that:
     (this.codePointAt(k) == ch) && (k <= fromIndex)     
     is true. 
	 
	 In either case, if no such character occurs in this
     string at or after position fromIndex, then
     -1 is returned.
	 不論哪種情況,如果在fromIndex之後,沒有這樣的字元出現了,那麼返回-1.
          
     There is no restriction on the value of fromIndex. If it
     is negative, it has the same effect as if it were zero: this entire
     string may be searched. If it is greater than the length of this
     string, it has the same effect as if it were equal to the length of
     this string: -1 is returned.
     
     <p>All indices are specified in {@code char} values
     (Unicode code units).
     
     @param   ch          a character (Unicode code point).
     @param   fromIndex   the index to start the search from.
     @return  the index of the first occurrence of the character in the
              character sequence represented by this object that is greater
              than or equal to {@code fromIndex}, or {@code -1}
              if the character does not occur.
     */
public int indexOf(int ch, int fromIndex) { final int max = value.length; if (fromIndex < 0) { fromIndex = 0; } else if (fromIndex >= max) { // Note: fromIndex might be near -1>>>1. return -1; } if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { // handle most cases here (ch is a BMP code point or a // negative value (invalid code point)) final char[] value = this.value; for (int i = fromIndex; i < max; i++) { if (value[i] == ch) { return i; } } return -1; } else { return indexOfSupplementary(ch, fromIndex); } }

發現這個indexOf(int ch, int fromIndex)的實際引數是int ch,int fromIndex,我們之所以寫成'a'也可以的原因就是:char型別的字元在底層都是可以轉換成int型的【學過c語言的人肯定會很熟悉】。示例如下:

    public static void test6(){
        int a = 'a';
        System.out.println(a);//97
        System.out.println((int)('b'));//98
    }