String字符串中獲取全部匹配結果的索引值

String字符串中獲取全部匹配結果的索引值

例如如今咱們有這樣一段代碼bash

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關鍵字出現的索引,那麼能夠這麼寫app

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;
    }
複製代碼

這樣調用便可ui

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);
        }
    }

複製代碼

輸出結果以下:spa

0
42
106
170

複製代碼
相關文章
相關標籤/搜索