Java全局搜索字符串

来源:互联网 发布:电影产业 知乎 编辑:程序博客网 时间:2024/05/22 14:53

Java全局搜索字符串

public class SearchString {    public static void main(String[] args) {        Scanner input=new Scanner(System.in);        System.out.print("请输入一段字符:");        String str=input.next();        System.out.print("请输入要查询的字符:");        String searchStr=input.next();        int fromIndex=0;//搜索的起始位置        System.out.print("\""+searchStr+"\""+"出现的位置是:");        while(true){            int index=str.indexOf(searchStr, fromIndex);            if(index==-1)//判断是否找到                break;            System.out.print(index+" ");             fromIndex=index+1;//重新指定搜索的起始位置         }        input.close();    }}
0 0
原创粉丝点击