9

I am trying to find how many times the word "Serotonin" appears in the gathered web data but cannot find a method for finding the number of times.

IEnumerator OnMouseDown()
{


    string GatheredData;
    StringToFind = "Serotonin"

    string url = "https://en.wikipedia.org/wiki/Dopamine";

    WWW www = new WWW(url);
    yield return www;
    GatheredData = www.text;


    //Attempted methods below

    M1_count = GatheredData.Contains(StringToFind);

    M1_count = GatheredData.Count(StringToFind);

    M1_count = GatheredData.IndexOf(StringToFind);



}

I can easily use the data from those methods 1 and 3 when I tell it what number in the index and method 2 would work but only works for chars not strings

I have checked online and on here but found nothing of finding the count of the StringToFind

No Strings on Me
  • 109
  • 1
  • 1
  • 8
  • GatheredData.IndexOf(string/char StringToFind,int starindex,int count); – No Strings on Me Aug 27 '18 at 01:27
  • oh true, the displacement method... that would work yes if no built in way – No Strings on Me Aug 27 '18 at 01:28
  • Preferring index of, how do I know how many other than indexof-ing until null? Or would that be the method? – No Strings on Me Aug 27 '18 at 01:30
  • 1
    Possible duplicate of [How to count of sub-string occurrences?](https://stackoverflow.com/questions/15577464/how-to-count-of-sub-string-occurrences) – mjwills Aug 27 '18 at 01:36
  • Depending on how many times you think the string "as" occurs in the string "aaaaaa" (I can imagine answers of 3 and 5), you may want to do things one way or another. – Flydog57 Aug 27 '18 at 01:36
  • Oops, autocorrect strikes again. Doing this on a phone on a Sunday evening can be a bad idea. That's "aa" into "aaaaaa". Boy, it's hard to fight off autocorrect sometimes. Thanks for the catch. – Flydog57 Aug 27 '18 at 01:59
  • 3
    Possible duplicate of [How would you count occurrences of a string (actually a char) within a string?](https://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-actually-a-char-within-a-string) – CAMD_3441 Aug 27 '18 at 02:18

6 Answers6

18

Assume string is like this

string test = "word means collection of chars, and every word has meaning";

then just use regex to find how many times word is matched in your test string like this

int count = Regex.Matches(test, "word").Count;

output would be 2

Saif
  • 2,611
  • 3
  • 17
  • 37
1

The solution int count = Regex.Matches(someString, potencialSubstring).Count;

did not work for me. Even thou I used Regex.Escape(str)

So I wrote it myself, it is quite slow, but the performance is not an issue in my app.

private static List<int> StringOccurencesCount(String haystack, String needle, StringComparison strComp)
{
  var results = new List<int>();
  int index = haystack.IndexOf(needle, strComp);
  while (index != -1)
  {
    results.Add(index);
    index = haystack.IndexOf(needle, index + needle.Length, strComp);
  }
  return results;
}

Maybe someone will find this useful.

  • 1
    Congratulations, you have just invented the square wheel ;-) The regex solution works but "potencialSubstring" is a regex pattern. `Matches (string input, string pattern);` and `Regex.Escape` does not escape every possible char. If you want an alternative why not using `string.Split`, then you don't need escape your potencialSubstring. – Kux Aug 17 '21 at 17:19
0

Improvement on @Petr Nohejl's excellent answer:

public static int Count (this string s, string substr, StringComparison strComp = StringComparison.CurrentCulture)
{
    int count = 0, index = s.IndexOf(substr, strComp);
    while (index != -1)
    {
        count++;
        index = s.IndexOf(substr, index + substr.Length, strComp);
    }
    return count;
}

This does not use Regex.Matches and probably has better performance and is more predictable.

See on .NET Fiddle

trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
0

If you don't worry about performance, here are 3 alternative solutions:

int Count(string src, string target)
    => src.Length - src.Replace(target, target[1..]).Length;

int Count(string src, string target)
    => src.Split(target).Length - 1;

int Count(string src, string target)
    => Enumerable
        .Range(0, src.Length - target.Length + 1)
        .Count(index => src.Substring(index, target.Length) == target);
Palindromer
  • 854
  • 1
  • 10
  • 29
-1

Oh yes, I have it now.

I will split() the array and get the length

2nd to that I will IndexOf until I return a -1

Thanks for help in the comments!

No Strings on Me
  • 109
  • 1
  • 1
  • 8
-1

A possible solution would be to use Regex:

var count = Regex.Matches(GatheredData.ToLower(), String.Format("\b{0}\b", StringToFind)).Count;
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Mehdi Ibrahim
  • 2,434
  • 1
  • 13
  • 13
  • You probably want to use `Regex.Escape(StringToFind)` instead. It's also probably worth mentioning that `\b` expects a boundary, so `SerotoninSerotonin` won't be matched, but `Serotonin Serotonin` will be. In the context of what OP wants, I think that's OK though :) – ProgrammingLlama Aug 27 '18 at 02:00
  • 1
    Exactly, my answer IS for what he exactly needs :) but thanks John! – Mehdi Ibrahim Aug 27 '18 at 02:35