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

来源:互联网 发布:java反射获得属性值 编辑:程序博客网 时间:2024/04/30 17:02

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

 (2012-04-06 10:50:27)
转载
标签: 

string

 

截取

 

位置

 

it

分类: JAVA_Discuss
public static int getCharacterPosition(String string){
    //这里是获取"/"符号的位置
    Matcher slashMatcher = Pattern.compile("/").matcher(string);
    int mIdx = 0;
    while(slashMatcher.find()) {
       mIdx++;
       //当"/"符号第三次出现的位置
       if(mIdx == 3){
          break;
       }
    }
    return slashMatcher.start();
 }
原创粉丝点击