String#Count Options

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 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.)

How String.count() works?

>>> help(str.count)
Help on method_descriptor:

count(...)
S.count(sub[, start[, end]]) -> int

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

Notice the non-overlapping.

Count multiple patterns using the str_count function in R

Do you mean the str_count function in the stringr package?

If so, it uses regular expressions and in the pattern for regular expressions the | character means "or", so str_count(mydf$string, 'apple|pear') will count the occurrences of "apple" or "pear" to give a total count. The string with the | characters can be constructed with paste, try:

str_count(mydf$string, paste(Uniques, collapse='|'))

You can see the string that is constructed by paste by just running that part of the code. Note that if you construct a pattern with a lot of options then it may run very slowly. Another option would be to split the 1st string into individual words and compare the vector of words with the vector of options using the %in% operator (then count the TRUE's).

Count the number of occurrences of a character in a string in Javascript

I have updated this answer. I like the idea of using a match better, but it is slower:

console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4

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", ".");

String count with overlapping occurrences

Well, this might be faster since it does the comparing in C:

def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count+=1
else:
return count

Count multiple letters in string Python

You probably mean s.count('l') + s.count('o').

The code you've pasted is equal to s.count('o'): the and operator checks if its first operand (in this case l) is false. If it is false, it returns its first operand (l), but it isn't, so it returns the second operand (o).

>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> True and 'x'
'x'
>>> False and 'x'
False
>>> 'x' and True
True
>>> 'x' and False
False
>>> 'x' and 'y'
'y'
>>> 'l' and 'o'
'o'
>>> s.count('l' and 'o')
2
>>> s.count('o')
2
>>> s.count('l') + s.count('o')
5

Official documentation



Related Topics



Leave a reply



Submit