您的位置:首页 > 编程语言 > Java开发

java.lang.String.indexOf()用法

2014-05-07 16:33 369 查看
java.lang.String.indexOf(char ch)

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

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

示例:

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'));
}
}


运行结果:

index of letter 's' = 3
index of letter 'e' = -1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: