Mega Code Archive

 
Categories / Java / Data Type
 

Count the number of occurrences of character c in a string

/**  * This software is provided as IS by Antilia-Soft SL.  * Copyright 2006-2007.  */ //package com.antilia.common.util; public class StringUtils {        /**    * counts the number of occurrences of character c in a string.    *     * @param str    * @param c    * @return int    */   public static int countChar(String str, char c) {     int start = -1;     int count = 0;     while (true) {       if ((start = str.indexOf(c, start + 1)) == -1)         return (count);       count++;     }   } }