String 中 的indexOf()

String 中 的indexOf()


package com.yiibai;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str = "This is yiibai";
    
    // returns the index of occurrence of character s
    System.out.println("index of letter 's' = " 
    + str.indexOf('s')); 
      
    // returns -1 as character e is not in the string
    System.out.println("index of letter 'e' = " 
    + str.indexOf('e'));
  }
}

运行结果

index of letter 's' = 3
index of letter 'e' = -1

String a="abc";

a.indexOf('a');  //0

a.indexOf('b');  //1

a.indexOf('c');  //2

a.indexOf('d');  //-1


a.indexOf('ab');  //0

a.indexOf('abc');  //0

就是制定字符串,第一次出现对比字符的排序(从0开始)

,如果没有就返回-1


你可能感兴趣的:(java基础)