美文网首页Java技术分享程序员
String字符串中获取所有匹配结果的索引值

String字符串中获取所有匹配结果的索引值

作者: 不学无数的程序员 | 来源:发表于2018-09-25 17:12 被阅读0次

String字符串中获取所有匹配结果的索引值

例如现在我们有这样一段代码


public interface ActErrorHisMapper {

    public List<ActError> getPage(Map<String, Object> params);

    public List<ActError> getList(Map<String, Object> params);

    public int getCount(Map<String, Object> params);
}

我们要查找所有的public关键字出现的索引,那么可以这么写

    public static List<Integer> findAllIndex(String string,int index,String findStr){
        List<Integer> list =new ArrayList<>();
        if (index != -1){
            int num = string.indexOf(findStr,index);
            list.add(num);
            //递归进行查找
            List myList = findAllIndex(string,string.indexOf(findStr,num+1),findStr);
            list.addAll(myList);
        }
        return list;
    }

这样调用即可

    public static void main(String[] args) {
        String string = "public interface ActErrorHisMapper {\n" + "\n"
                + "    public List<ActError> getPage(Map<String, Object> params);\n" + "\n"
                + "    public List<ActError> getList(Map<String, Object> params);\n" + "\n"
                + "    public int getCount(Map<String, Object> params);\n" + "}";
        List<Integer> num = findAllIndex(string,0,"public");
        for (Integer integer : num){
            System.out.println(integer);
        }
    }

输出结果如下:

0
42
106
170

相关文章

  • String字符串中获取所有匹配结果的索引值

    String字符串中获取所有匹配结果的索引值 例如现在我们有这样一段代码 我们要查找所有的public关键字出现的...

  • js字符串方法大全

    string对象构造函数 concat方法 连接字符串 indexOf 获取索引值 找到匹配项返回索引值,如果没找...

  • js 匹配URL的正则表达式

    待匹配的字符串 获取字符串中所有 a 标签的 href 的值及其中间的文本内容 输出结果: 匹配所有的 img 标...

  • 基础(一) String 对象方法

    String 对象 基础 方法 整理 str.charAt(index) 获取字符串索引的值。 str.fixed...

  • JavaScript 匹配关键字

    string: (string).match;返回值:匹配到的字符串; Array:[数组].includes返回...

  • JavaScript 中 String 的方法

    获取String中字符的三种方式 charAt(position)获取字符串中position位置的字符,索引从0...

  • swift5.0 字符串截取

    字符串索引 每一个 String 值都有一个关联的索引(index)类型,String.Index,它对应着字符串...

  • swift5.0 字符串截取

    字符串索引 每一个 String 值都有一个关联的索引(index)类型,String.Index,它对应着字符串...

  • string对象

    (1)、字符串中每个字符都有对应的索引值 索引值从0开始 (2)、获取字符串的长度: 打点 le...

  • 性能优化

    MySQL中使用索引的典型场景 匹配全值对索引中所有列都指定具体值,即是对索引中的所有列都有等值匹配的条件。 如我...

网友评论

    本文标题:String字符串中获取所有匹配结果的索引值

    本文链接:https://www.haomeiwen.com/subject/pgmloftx.html