Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many times a char appears in a string? 4

Status
Not open for further replies.

meinhunna

Programmer
Jul 31, 2004
118
0
0
AU
how can i find, how many times a character appears in a string? for eg how many time "-" occurs in "123-456-78"

thanx
 
Code:
Public Shared Function CountChars(ByVal sString As String, ByVal sSearch As String) As Integer
   Dim i As Integer = sString.Length - sString.Replace(sSearch, "").Length
   Return i
End Function


Sweep
...if it works dont mess with it
 
Code:
   Dim s1 As String = "Aaa Ha"
   Dim r As RegularExpressions.Regex = _
     New RegularExpressions.Regex("a", RegularExpressions.RegexOptions.IgnoreCase)
   Dim matches As Integer = r.Matches(s1).Count
   MessageBox.Show(matches)   'should display 4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top