In order to find the number of
occurences of a character or a special character in a LENGTHY STRING or even a
regular String,anyone can use this code which is very effective and very fast.
This reduces the manipulation of the Lengthy string for finding the number of occurences.
This reduces the manipulation of the Lengthy string for finding the number of occurences.
public static int getNoOfOccurrences(String originalString, String characterToBeSearched, String strForSplit, String strForLastIndex) { int noOfOccurrences = 0; int lastIndex = originalString.lastIndexOf(strForLastIndex); int stringLength = originalString.length(); if (stringLength == lastIndex + 1) { originalString = originalString + " "; } String strInArray[] = originalString.split(strForSplit); noOfOccurrences = strInArray.length - 1; return noOfOccurrences; }