String类中一些常用的函数

时间:2021-08-30 19:15:15

1 CharAt(index) : 通过他的索引来获取元素

@Test
public void test1(){
String a="dfjkdjfd134";
for(int i=0;i<a.length();i++){
System.out.print(a.charAt(i)+" ");
} }
答案:d f j k d j f d 1 3 4

2  indexOf(String s) : 返回当前字符串的索引,字符串索引的位置从0开始

  indexOf(String s,int startpoint) 方法是从当前字符串的startpoint位置开始索引字符串s

String tom="I am a good cat"indexOF(String;
tom.indexOf("a"); // 值是2
tom.indexOf("good",); //值是7
tom.indexOf("a",); //值是13
tom.index.of("w",); //值是-1