Java 获取字符串中第N次出现的字符位置

来源:互联网 发布:淘宝搜索排名优化规则 编辑:程序博客网 时间:2024/05/21 11:18
public static int getCharacterPosition(String string){
    //这里是获取"/"符号的位置
    Matcher slashMatcher = Pattern.compile("/").matcher(string);
    int mIdx = 0;
    while(slashMatcher.find()) {
       mIdx++;

       //当"/"符号第k次出现的位置

          int N=k;

       if(mIdx == N){
          break;
       }
    }
    return slashMatcher.start();
 }
0 0
原创粉丝点击