66

I simply have a string that looks something like this:

"7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false"

All I want to do is to count how many times the string "true" appears in that string. I'm feeling like the answer is something like String.CountAllTheTimesThisStringAppearsInThatString() but for some reason I just can't figure it out. Help?

Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
Paul Mignard
  • 5,824
  • 6
  • 44
  • 60
  • 2
    http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c/542136#542136 – jball Jun 10 '10 at 16:59
  • 8
    @jball I think all the solutions there are for counting occurrences of a *character* – AakashM Jun 10 '10 at 17:56
  • @AakashM, look at the second link. Also, this is the perfect opportunity to provide a more correct (i.e., string based, not char based) answer to that question. – jball Jun 10 '10 at 18:46
  • Refer http://www.dotnetperls.com/string-occurrence – Syed Mohamed Aug 14 '14 at 09:05
  • 5
    I'm glad you don't work for Microsoft and get to name methods! – user3791372 Sep 28 '16 at 10:18
  • 1
    The linked questions ("This question already has answers here: ") has mostly answers for counting multiple character occurrences, not multiple substring occurrences. – Alex P. Jun 07 '23 at 13:56

7 Answers7

208
Regex.Matches(input, "true").Count
Chris Benard
  • 3,167
  • 2
  • 29
  • 35
µBio
  • 10,668
  • 6
  • 38
  • 56
17

Probably not the most efficient, but think it's a neat way to do it.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(CountAllTheTimesThisStringAppearsInThatString("7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false", "true"));
        Console.WriteLine(CountAllTheTimesThisStringAppearsInThatString("7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false", "false"));

    }

    static Int32 CountAllTheTimesThisStringAppearsInThatString(string orig, string find)
    {
        var s2 = orig.Replace(find,"");
        return (orig.Length - s2.Length) / find.Length;
    }
}
rjdevereux
  • 1,842
  • 2
  • 21
  • 35
14

Your regular expression should be \btrue\b to get around the 'miscontrue' issue Casper brings up. The full solution would look like this:

string searchText = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
string regexPattern = @"\btrue\b";
int numberOfTrues = Regex.Matches(searchText, regexPattern).Count;

Make sure the System.Text.RegularExpressions namespace is included at the top of the file.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
DonaldRay
  • 503
  • 3
  • 11
5

This will fail though if the string can contain strings like "miscontrue".

   Regex.Matches("7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false", "true").Count;
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
Sploofy
  • 493
  • 5
  • 13
4

Here, I'll over-architect the answer using LINQ. Just shows that there's more than 'n' ways to cook an egg:

public int countTrue(string data)
{
    string[] splitdata = data.Split(',');

    var results = from p in splitdata
            where p.Contains("true")
            select p;

    return results.Count();
}
Robaticus
  • 22,857
  • 5
  • 54
  • 63
3

With Linq...

string s = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
var count = s.Split(new[] {',', ':'}).Count(s => s == "true" );
Jace Rhea
  • 4,982
  • 4
  • 36
  • 55
2

do this , please note that you will have to define the regex for 'test'!!!

string s = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
string[] parts = (new Regex("")).Split(s);
//just do a count on parts
VoodooChild
  • 9,776
  • 8
  • 66
  • 99