java-字符串查找

来源:互联网 发布:角球数据查询 编辑:程序博客网 时间:2024/06/14 02:32

使用indexOf()查找:str.indexOf(要查找的字符串,int 查找的起始位置)

code:

public class first{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));}}

使用lastindexOf()查找,从右往左查找,如果不指定起始索引,默认从末尾开始

public class first{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("使用lastIndexOf(\"o\",6)结果:"+words.lastIndexOf("o",6));}}


根据索引查找charAt(索引值)直接查找字符

public class first{public static void main(String[] args){String words="today,monday,sunday";System.out.println(words.charAt(0));System.out.println(words.charAt(1));System.out.println(words.charAt(8));}}




0 0