java.lang.String.indexOf()用法

时间:2014-05-08 13:02:04   收藏:0   阅读:423

java.lang.String.indexOf(char ch)

方法返回字符ch在指定字符串中第一次出现的下标索引位置

如果字符ch在指定的字符串中找不到,则返回-1

示例:

bubuko.com,布布扣
import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str = "This is tanglc‘s cnblog";
    
    // returns the index of character s appear in the first time
    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‘));
  }
}
bubuko.com,布布扣

运行结果:

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

 

java.lang.String.indexOf()用法,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!