To count a character in a string in C# and VB.NET you can use the following snippet.

Sample C#

public static Int64 CountChar(this string input, char c)
{
	return input.Count(t => c == t);
}

Sample VB.NET

Public Shared Function CountChar(input As String, c As Char) As Int64
	Return input.Count(Function(t) c = t)
End Function

One thought on “How to count a character in a string in C# and VB.NET”

Leave a Reply