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

简介: String字符串中获取所有匹配结果的索引值例如现在我们有这样一段代码public interface ActErrorHisMapper { public List getPage(Map params); public L...

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

相关文章
|
2天前
|
存储 缓存 测试技术
CMake String函数:如何巧妙地在cmake中操作字符串
CMake String函数:如何巧妙地在cmake中操作字符串
167 0
|
2天前
|
存储 编译器 Linux
【字符串探秘:手工雕刻的String类模拟实现大揭秘】(下)
【字符串探秘:手工雕刻的String类模拟实现大揭秘】
|
2天前
|
存储 XML 缓存
Java字符串内幕:String、StringBuffer和StringBuilder的奥秘
Java字符串内幕:String、StringBuffer和StringBuilder的奥秘
26 0
|
2天前
|
C++
c++:string相关的oj题(把字符串转换成整数、344.反转字符串、387. 字符串中的第一个唯一字符、917. 仅仅反转字母)
c++:string相关的oj题(把字符串转换成整数、344.反转字符串、387. 字符串中的第一个唯一字符、917. 仅仅反转字母)
56 0
string(字符串)
在 Lua 中,字符串可以用双引号或单引号定义,如 `string1 = &quot;this is string1&quot;` 和 `string2 = &#39;this is string2&#39;`。多行字符串可由两个方括号包围,例如 `html` 变量所示,它包含了一个 HTML 片段。Lua 会尝试将数字字符串转换为数值进行算术运算,但混合字符串和数字可能导致错误,如 `&quot;error&quot; + 1`。
|
2天前
|
C++ 索引
c++:string相关的oj题(415. 字符串相加、125. 验证回文串、541. 反转字符串 II、557. 反转字符串中的单词 III)
c++:string相关的oj题(415. 字符串相加、125. 验证回文串、541. 反转字符串 II、557. 反转字符串中的单词 III)
44 0
|
2天前
|
缓存 安全 Java
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
9 0
|
2天前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
2天前
|
JavaScript
js 字符串String转对象Object
该代码示例展示了如何将一个以逗号分隔的字符串(`&#39;1.2,2,3,4,5&#39;`)转换为对象数组。通过使用`split(&#39;,&#39;)`分割字符串并`map(parseFloat)`处理每个元素,将字符串转换成浮点数数组,最终得到一个对象数组,其类型为`object`。
|
2天前
|
Python
Python中的字符串(String)
【4月更文挑战第6天】Python字符串是不可变的文本数据类型,可使用单引号或双引号创建。支持连接(+)、复制(*)、长度(len())、查找(find()、index()、in)、替换(replace())、分割(split())、大小写转换(lower()、upper())和去除空白(strip()等)操作。字符串可格式化,通过%操作符、`str.format()`或f-string(Python 3.6+)。字符串以Unicode编码,作为对象拥有属性和方法。熟悉这些操作对处理文本数据至关重要。
39 6
Python中的字符串(String)