str.indexOf(value) str.indexOf(value,int fromIndex)
String s = "Hello Java";
int size = s.indexOf('v'); // size的结果为8
public static void main(String[] args) {
String words = "today,monday,sunday";
System.out.println("原始字符串是'"+words+"'");
System.out.println("indexOf(\"day\")结果:"+words.indexOf("day"));
System.out.println("indexOf(\"day\",5)结果:"+words.indexOf("day",5));
System.out.println("indexOf(\"o\")结果:"+words.indexOf("o"));
System.out.println("indexOf(\"o\",6)结果:"+words.indexOf("o",6));
}
运行后的输出结果如下:
原始字符串是'today,monday,sunday'
indexOf("day")结果:2
indexOf("day",5)结果:9
indexOf("o")结果:1
indexOf("o",6)结果:7
str.lastIndexOf(value) str.lastlndexOf(value, int fromIndex)
public static void main(String[] args) {
String words="today,monday,Sunday";
System.out.println("原始字符串是'"+words+"'");
System.out.println("lastIndexOf(\"day\")结果:"+words.lastIndexOf("day"));
System.out.println("lastIndexOf(\"day\",5)结果:"+words.lastIndexOf("day",5));
System.out.println("lastIndexOf(\"o\")结果:"+words.lastIndexOf("o"));
System.out.println("lastlndexOf(\"o\",6)结果:"+words.lastIndexOf("o",6));
}
运行后的输出结果如下:
原始字符串是'today,monday,Sunday'
lastIndexOf("day")结果:16
lastIndexOf("day",5)结果:2
lastIndexOf("o")结果:7
lastlndexOf("o",6)结果:1
字符串名.charAt(索引值)提示:字符串本质上是字符数组,因此它也有索引,索引从零开始。
String words = "today,monday,sunday"; System.out.println(words.charAt(0)); // 结果:t System.out.println(words.charAt(1)); // 结果:o System.out.println(words.charAt(8)); // 结果:n
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有