String类的indexOf()方法

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/83023042

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
    }

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/83023042