Android Char Value Check getNthIndexOf(char c, String str, int n)

Here you can find the source of getNthIndexOf(char c, String str, int n)

Description

find the nth index of a char c in a string.

Parameter

Parameter Description
c a parameter
n a parameter

Declaration

public static int getNthIndexOf(char c, String str, int n) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  w  w  . j a v a 2 s  .c  om*/
     * find the nth index of a char c in a string.  returns -1 if there aren't that many chars in the string
     * @param c
     * @param n
     * @return
     */
    public static int getNthIndexOf(char c, String str, int n) {

        if (n < 1) {
            throw new IllegalArgumentException("n must be >= 1: " + n);
        }

        int index = str.indexOf(c);
        int count = 0;
        while (index != -1 && (++count) < n) {
            index = str.indexOf(c, index + 1);
        }

        return index;
    }
}

Related

  1. isWordSeparator(char c)
  2. isKana(char character)
  3. isWordSeparator(char c, char[] wordSeparators)
  4. getSpecialCharLength(char c)
  5. getCharsLength(char[] chars, int specialCharsLength)
  6. displayWidth(char ch)
  7. getCharType(char c)
  8. isAlef(char c)
  9. isArabic(char c)