Counting the Number of Occurrences of a String Within a String

Count the number of occurrences of a character in a string

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4

How to count number of occurrences of a substring inside a string in Python?

you can use count

print("hellohel".count("hel"))
2

If you want to count overlapping occurrences... maybe this can help

def countOverlapping(string, item):
count = 0
for i in range(0,len(string)):
if item in string[i:len(item)+i]:
count += 1
return count

print(countOverlapping("ehehe", "ehe"))

output should be...

2

How does that work?

as @SomeDude mentioned it uses what he calls a sliding window approach

we take the length of the substring and check if its in that "window" of the string each iteration:

is ehe in [ehe]he? yes, count += 1
is ehe in e[heh]e? no, pass
is ehe in eh[ehe]? yes, count += 1

Occurrences of substring in a string

The last line was creating a problem. lastIndex would never be at -1, so there would be an infinite loop. This can be fixed by moving the last line of code into the if block.

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;

while(lastIndex != -1){

lastIndex = str.indexOf(findStr,lastIndex);

if(lastIndex != -1){
count ++;
lastIndex += findStr.length();
}
}
System.out.println(count);

How to count string occurrence in string?

The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches is twice:

var temp = "This is a string.";var count = (temp.match(/is/g) || []).length;console.log(count);

Count the number of Strings in a String

if (!text.contains(symbol)) {
break;
}
count++;
if ((text.indexOf(symbol, 1)) == -1) {
break;
}

This part of your code is quite problematic. You increment the count if the symbol is present in the text, even if it is the first character - while your

text = text.substring(text.indexOf(symbol, 1));

code assures that the first character is the specified symbol in the text. This causes your code to overcount by one if it contains the specified symbol. To fix it, change your code to

while (true) {
if (!text.contains(symbol)) {
break;
}
count++;
text = text.substring(text.indexOf(symbol)+1);//+1 so it starts at the next character
System.out.println(text);
System.out.println(count);
}

As you can see, the first character is no longer the specified symbol. This ensures that

  1. The contains() method is enough to determine the presence of the symbol
  2. The count is set correctly

Count occurrences of a substring in a list of strings

You can do this by using the sum built-in function. No need to use list.count as well:

>>> data = ["the foo is all fooed", "the bar is all barred", "foo is now a bar"]
>>> sum('foo' in s for s in data)
2
>>>

This code works because booleans can be treated as integers. Each time 'foo' appears in a string element, True is returned. the integer value of True is 1. So it's as if each time 'foo' is in a string, we return 1. Thus, summing the 1's returned will yield the number of times 1 appeared in an element.

A perhaps more explicit but equivalent way to write the above code would be:

>>> sum(1 for s in data if 'foo' in s)
2
>>>

How do I count the number of occurrences of a char in a String?

My 'idiomatic one-liner' for this is:

int count = StringUtils.countMatches("a.b.c.d", ".");

Why write it yourself when it's already in commons lang?

Spring Framework's oneliner for this is:

int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");

How would you count occurrences of a string (actually a char) within a string?

If you're using .NET 3.5 you can do this in a one-liner with LINQ:

int count = source.Count(f => f == '/');

If you don't want to use LINQ you can do it with:

int count = source.Split('/').Length - 1;

You might be surprised to learn that your original technique seems to be about 30% faster than either of these! I've just done a quick benchmark with "/once/upon/a/time/" and the results are as follows:

Your original = 12s

source.Count = 19s

source.Split = 17s

foreach (from bobwienholt's answer) = 10s

(The times are for 50,000,000 iterations so you're unlikely to notice much difference in the real world.)



Related Topics



Leave a reply



Submit