String字符串中获取所有匹配结果的索引值
生活随笔
收集整理的這篇文章主要介紹了
String字符串中获取所有匹配结果的索引值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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字符串中获取所有匹配结果的索引值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快手极速版在哪里下载(上的快手极速版)
- 下一篇: 王者荣耀武道币在哪看