jaguRita

jaguRita

  • NA
  • 40
  • 0

No of Occurances of String

Aug 7 2007 2:38 PM

How can i find out of Number of occurances in string?I have used array to find out.Is there any other way (Use of IndexOf) to find out?

string s = "Apple"; //No of occurances of  'p'

char[] c = s.ToCharArray();

int count = 0;

for(int i=0;i<c.Length;i++)

{

      if (c[i] = = 'p')

      {   count++;      }

}

Console.Write(count);

Thanks.


Answers (3)