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

String 中 的indexOf()

2015-12-15 11:31 579 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息