2009-07-15 33 views

回答

15

您可以使用正则表达式就像在Java SE:

Pattern pattern = Pattern.compile(".* (Ka).*"); 
Matcher matcher = pattern.matcher("Manoj Kumar Kashyap"); 
if(matcher.matches()) 
{ 
    int idx = matcher.start(1); 
}
4

你并不需要一个正则表达式来做到这一点。我不是一个Java专家,但据Android docs

公众诠释的indexOf(字符串字符串)
搜索这个字符串的第 指数为指定字符串。搜索该字符串的 开始于 开始,并且移动到该字符串的末尾 。

参数
字符串要查找的字符串。

返回
指定字符串的第一个字符 在 索引这个字符串,-1,如果指定 字符串不是子串。

你可能会拥有类似:

int index = somestring.indexOf(" Ka"); 
0

如果你真的需要正则表达式,而不只是indexOf,它可能不喜欢这样

String[] split = "Manoj Kumar Kashyap".split("\\sKa"); 
if (split.length > 0) 
{ 
    // there was at least one match 
    int startIndex = split[0].length() + 1; 
}