myString.Count(ch => ch == 'C');
// given the following, and assuming you don't care about case-sensitivity: string myString = "Download"; char myChar = 'o'; int count = 0; // if case-sensitivity matters, comment these two lines myChar = myChar.ToLower(); myString = myString.ToLower() // do this: foreach (char c in myString) { count += (c = myChar) ? 1 : 0; } // or this: count = myString.Count(ch => ch == myChar); // or this: int count = (from c in myString where c == myChar select c).Count();
Dim s As String = "How are you doing?" Dim Count As Integer = (From c As Char In s.ToCharArray Where c.Equals("o"c) Select c).Count
Public Function CountChar(ByVal str As String, ByVal Ch1 As Char) As Integer Dim iCnt As Integer = 0 For Each c As Char In str If c = Ch1 Then iCnt += 1 Next CountChar = iCnt End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)