Find the nth occurrence of a specific character in a String

edited July 2017 in How To...

I am trying to write a function that returns the integer index value for the nth occurrence of a specified character c, like:

int findNthChar(String a, char c, int n)

I know to use a while loop but am not too sure of the code within it.

Answers

  • edited July 2017 Answer ✓
    1. https://Processing.org/reference/String_indexOf_.html
    2. http://Docs.Oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-int-int-

    /** 
     * Nth Char in Str (v1.1)
     * GoToLoop (2017/Jul/25)
     *
     * Forum.Processing.org/two/discussion/23582/
     * find-the-nth-occurrence-of-a-specific-character-in-a-string#Item_1
     */
    
    static final String TEST = "Find the nth occurr specific char in a String.";
    static final char CH = 'n';
    static final int NTH = 3;
    
    void setup() {
      println(TEST);
      print("Len: " + TEST.length());
      println(" \tChar: " + CH + " \tNth: " + NTH);
      println("Index: " + findIdxOfNthChar(TEST, CH, NTH));
      exit();
    }
    
    static final int findIdxOfNthChar(final String s, final char ch, final int n) {
      int i = 0, idx = -1;
      while (i++ < n)  if ((idx = s.indexOf(ch, idx + 1)) < 0)  return -1;
      return idx;
    }
    
  • Similar to this I also need to count how many times a specific character occurs in a String. Do I just need to remove the while statement or is there more to it?

  • edited July 2017
    /** 
     * Nth Char in Str (v2.1)
     * GoToLoop (2017/Jul/26)
     *
     * Forum.Processing.org/two/discussion/23582/
     * find-the-nth-occurrence-of-a-specific-character-in-a-string#Item_3
     */
    
    static final String TEST = "Find the nth occurr specific char in a String.";
    static final char CH = 'n';
    static final int NTH = 3;
    
    void setup() {
      println(TEST);
      print("Len: " + TEST.length());
      println(" \tChar: " + CH + " \tNth: " + NTH);
      print("Index: " + findIdxOfNthChar(TEST, CH, NTH));
      println(" \tFound: " + findCharOccurs(TEST, CH));
      exit();
    }
    
    static final int findIdxOfNthChar(final String s, final char ch, final int n) {
      int i = 0, idx = -1;
      while (i++ < n)  if ((idx = s.indexOf(ch, idx + 1)) < 0)  return -1;
      return idx;
    }
    
    static final int findCharOccurs(final String s, final char ch) {
      int n = 0, idx = -1;
      while ((idx = s.indexOf(ch, idx + 1)) >= 0)  ++n;
      return n;
    }
    
  • edited July 2017

    I now have to utilize the findNthChar():

    /********************
    *  Searches thtrough the string, locates the nth term of the character c. and returns
    *  the index of that character, using a while loop that starts at the beginning of the
    *  String and scans forward.
    ********************/
    int findNthChar(String a, char c, int n)
    {
      int w = 0; //intial value for while loop
      int idx = -1; //initial index value
      while (w++ < n)
      {
        if ((idx = a.indexOf(c, idx+1)) < 0)
        {
          return -1;
        }
      }
      return idx;
    }
    

    and the stringFront():

    /********************
    *  Returns the first count characters of the String as a new String.
    ********************/
    String stringFront(String s, int count)
    {
      String ss = s.substring(0,count);
      return ss;
    }
    

    to create the getFirstWords():

    /********************
    *  Returns a new String containing the first n words in s, where the character delimiter
    *  defines a word boundary, using stringFront to get the actual words as a new String.
    ********************/
    String getFirstWords(String s, int n, char delimiter)
    {
      String firstWord = stringFront(s, findNthChar(s, delimiter, n));
      return firstWord;
    }
    

    Am I close with getFirstWords()?

  • I don't think so.

    Returns a new String containing the first n words in s

    words is plural

  • edited July 2017
    /** 
     * Nth Char in Str (v3.1)
     * GoToLoop (2017/Jul/26)
     *
     * Forum.Processing.org/two/discussion/23582/
     * find-the-nth-occurrence-of-a-specific-character-in-a-string#Item_6
     */
    
    static final String TEST = "Find the nth occurr specific char in a String.";
    static final char CH = 'n';
    static final int NTH = 3, WORDS = 4;
    
    void setup() {
      println(TEST);
      print("Len: " + TEST.length());
      println(" \tChar: " + CH + " \tNth: " + NTH);
      print("Index: " + findIdxOfNthChar(TEST, CH, NTH));
      println(" \tFound: " + findCharOccurs(TEST, CH) + ENTER);
    
      print("Words Total: " + getNumberOfWords(TEST, ' '));
      println(" \tWords Requested: " + WORDS);
      println("Words: " + getFirstWords(TEST, ' ', WORDS));
    
      exit();
    }
    
    static final int findIdxOfNthChar(final String s, final char ch, final int n) {
      int i = 0, idx = -1;
      while (i++ < n)  if ((idx = s.indexOf(ch, idx + 1)) < 0)  return -1;
      return idx;
    }
    
    static final int findCharOccurs(final String s, final char ch) {
      int n = 0, idx = -1;
      while ((idx = s.indexOf(ch, idx + 1)) >= 0)  ++n;
      return n;
    }
    
    static final String stringFront(final String s, final int n) {
      return s.substring(0, n);
    }
    
    static final int getNumberOfWords(final String s, final char ch) {
      return split(s, ch).length;
    }
    
    static final String getFirstWords(final String s, final char ch, final int n) {
      final String[] found  = split(s, ch);
      final int number = min(abs(n), found.length);
      final String[] firsts = subset(found, 0, number);
      return join(firsts, ch);
    }
    
Sign In or Register to comment.