6

I am trying to find the count of the substring in a big string of length 10000 characters. Finally I need to remove all the substring in it. example s = abacacac, substr = ac, num of occurrence = 3 and final string is s = ab. My code is below, its not efficient for data of length 10000 characters.

int count =0;
while(s.contains(substr))
{
   s= s.replaceFirst(substr,"");
   count++;    
}
5
  • In s = abacacac, substr = ac, num of occurrence = 3 and final string is s = ab, right? isn't it?? Commented Aug 25, 2017 at 20:00
  • corrected sorry about that
    – ajayramesh
    Commented Aug 25, 2017 at 20:01
  • 1
    In 'aacacc' your result is 4 ocurrences and final is "" Commented Aug 25, 2017 at 20:01
  • Suppose you just replace all the occurrences with replace, and then look at how much shorter your string has become to work out how many times it was replaced?
    – khelwood
    Commented Aug 25, 2017 at 20:02
  • 1
    Be careful of substrings that contain parts of themselves. like "ana" in banana. Many of the solutions below will fail in this case.
    – Paul Rubel
    Commented Oct 5, 2018 at 14:18

5 Answers 5

21

What about:

String temp = s.replace(sub, "");
int occ = (s.length() - temp.length()) / sub.length();

Just remove all the substring, then check the difference on string length before and after removal. Divide the temp string with number of characters from the substring gives you the occurrences.

1
  • 1
    With this, remember you want to validate that s is not null, sub is not null, and sub.length() is not 0, and throw an invalid argument error (much easier to debug if you throw your own error)
    – Tezra
    Commented Aug 25, 2017 at 20:28
5

For countung the substrings I would use indexOf:

int count = 0;
for (int pos = s.indexOf(substr); pos >= 0; pos = s.indexOf(substr, pos + 1))
    count++;
1

To count the matching substring

System.out.println(s.split(substr, -1).length-1);

To get replaced string- you can use following code

System.out.println(Pattern.compile(s).matcher(substr).replaceAll(""));

0

Here's a method I made that should work perfectly right out of the box without throwing any errors,

    private static int countMatches(String str, String sub) {
    int count = 0;
    if(!str.isEmpty() && !sub.isEmpty()) {
        for (int i = 0; (i = str.indexOf(sub, i)) != -1; i += sub.length()) {
            count++;
        }
    }
    return count;
}

I will now continue to explain what the method does for beginners.

We start at the count 0. Then we check if both of our strings are NOT empty, knowing they aren't empty we continue with counting for our sub-string, we make a simple loop that counts the sub-string and the loop ends once indexOf returns -1 which means the sub-string is not found.

Just copy and paste it to your project and run it via executing

int count = countMatches("Hello World", "World");

now count should return the index of 1 if its being executed.

Happy coding :)

0

To count the matching substring

public class CountOccurrencesOfSubstringExample {
 
    public static void main(String[] args) {
        
        String str = "JavaExamplesJavaCodeJavaProgram";
        
        String strFind = "Java";
        int count = 0, fromIndex = 0;
        
        while ((fromIndex = str.indexOf(strFind, fromIndex)) != -1 ){
 
            System.out.println("Found at index: " + fromIndex);
            count++;
            fromIndex++;
            
        }
        
        System.out.println("Total occurrences: " + count);
    }
}

output:

Found at index: 0
Found at index: 12
Found at index: 20
Total occurrences: 3

Not the answer you're looking for? Browse other questions tagged or ask your own question.