How do I create a regular expression for this in android?

前端 未结 3 1841
野的像风
野的像风 2020-12-17 22:26

Suppose I have a string like this:

string = \"Manoj Kumar Kashyap\";

Now I want to create a regular expression to match where Ka appears af

相关标签:
3条回答
  • 2020-12-17 22:45

    You can use regular expressions just like in Java SE:

    Pattern pattern = Pattern.compile(".* (Ka).*");
    Matcher matcher = pattern.matcher("Manoj Kumar Kashyap");
    if(matcher.matches())
    {
        int idx = matcher.start(1);
    }
    0 讨论(0)
  • 2020-12-17 22:46

    If you really need regular expressions and not just indexOf, it's possible to do it like this

    String[] split = "Manoj Kumar Kashyap".split("\\sKa");
    if (split.length > 0)
    {
        // there was at least one match
        int startIndex = split[0].length() + 1;
    }
    
    0 讨论(0)
  • 2020-12-17 22:57

    You don't need a regular expression to do that. I'm not a Java expert, but according to the Android docs:

    public int indexOf (String string)
    Searches in this string for the first index of the specified string. The search for the string starts at the beginning and moves towards the end of this string.

    Parameters
    string the string to find.

    Returns
    the index of the first character of the specified string in this string, -1 if the specified string is not a substring.

    You'll probably end up with something like:

    int index = somestring.indexOf(" Ka");
    
    0 讨论(0)
提交回复
热议问题