Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the count of substring in java

Tags:

java

string

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++;    
}
like image 623
ajayramesh Avatar asked Aug 25 '17 19:08

ajayramesh


People also ask

How do you count the number of substrings in a string?

Total number of substrings = n + (n - 1) + (n - 2) + (n - 3) + (n - 4) + ……. + 2 + 1. So now we have a formula for evaluating the number of substrings where n is the length of a given string.

How many substrings are in a string of length n?

Approach: The count of sub-strings of length n will always be len – n + 1 where len is the length of the given string.

How many times a substring appears in a string?

count() One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

How to count occurrences of substring in string in Java?

There are several ways using which you can count occurrences of substring in Java. You can count occurrences of substring in string using indexOf method of String class.

How to substring a string in Java?

Substring in Java 1 String substring () : This method has two variants and returns a new string that is a substring of this string. The... 2 String substring (begIndex, endIndex): This method has two variants and returns a new string that is a substring of... More ...

How to find the index of a substring in a string?

The indexOf () method in java is a specialized function to find the index of the first occurrence of a substring in a string. This method has 4 overloads. We will use the second overload as we have to check the entire string. The fromIndex parameter is used to specify the starting index from where to start the search.

How to check if a substring is present in a string?

In the while loop, we find the substring, assign the index of next occurrence to fromIndex and check if the returned value is greater than -1. The indexOf method returns -1 if the substring is not found in the string, otherwise, it returns the index of the substring. Inside the while loop, we increment the count of occurrence of substring.


2 Answers

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.

like image 138
user3437460 Avatar answered Oct 15 '22 13:10

user3437460


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++;
like image 20
Dorian Gray Avatar answered Oct 15 '22 13:10

Dorian Gray