Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How many special characters are there in a string

    Hi!
    I want to figure out that how many special characters are there in a string.
    But I can't find a solution. Here is my code.

    Code:
    Sub CountChar()
    Dim SearchString, SearchChar, MyPos
    SearchString = "Character"
    SearchChar1 = "a"
    SearchChar2 = "r"
    
    MyPos1 = InStr(1, SearchString, SearchChar1)
    MyPos2 = InStr(1, SearchString, SearchChar2)
    MsgBox "There are  " & MyPos1 & " (a) and " & MyPos2 & " (r) characters in your string"
    
    End Sub
    It should be "2 a" and "2 r"

  2. #2
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    InStr returns the position of the character, not the count. Try this:

    Code:
    Sub CountChar()
    Dim SearchString As String, SearchChar1 As String, SearchChar2 As String
    Dim MyPos1 As Integer
    Dim MyPos2 As Integer
    SearchString = "Character"
    SearchChar1 = "a"
    SearchChar2 = "r"
    
    MyPos1 = Len(SearchString) - Len(Replace(SearchString, SearchChar1, ""))
    MyPos2 = Len(SearchString) - Len(Replace(SearchString, SearchChar2, ""))
    
    MsgBox "There are  " & MyPos1 & " (a) and " & MyPos2 & " (r) characters in your string"
    
    End Sub
    This code calculates the length of SearchString, minus the length of SearchString after replacing the specificed characters with an empty string (i.e. nothing).

  3. The Following User Says Thank You to richardbarrett For This Useful Post:

    yken (2013-06-29)

  4. #3
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you very much.

  5. #4
    New Lounger
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's another way to do the same thing that may be a little easier to read and understand. Calling the 'CountChars' function like this: CountChars("food", "o") will return the integer '2'. Note that this also works if 'search_for' is more than one character. So CountChars("foodood", "ood") returns 2 as well

    Code:
    Public Function CountChars(this_string As String, search_for As String)
    Dim count As Integer
    Dim i As Integer
        i = 1
        While i < Len(this_string) + 1
            If Mid(this_string, i, Len(search_for)) = search_for Then
                count = count + 1
            End If
            i = i + 1
        Wend
        CountChars = count
    End Function

  6. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Note that this also works if 'search_for' is more than one character. So CountChars("foodood", "ood") returns 2 as well
    It's a rare case, but you could have copies of search_for overlapping in this_string. Using a similar example, how many instances of "oo" appear in "fooood"? Initially you might think there are two copies, but CountChars will correctly return 3, because it sees fooood, fooood, and fooood.

    If you want the code to agree with the "illogical human" perception, the code should be this:

    Code:
    Public Function CountChars(this_string As String, search_for As String)
    Dim count As Integer
    Dim i As Integer
        i = 1
        While i < Len(this_string) + 1
            If Mid(this_string, i, Len(search_for)) = search_for Then
                count = count + 1
                i = i + Len(search_for)
            Else
                i = i + 1
            End If
        Wend
        CountChars = count
    End Function
    A similar code that will run faster is to use the Instr function instead of stepping through this_string one character at a time.
    Last edited by jjfreedman; 2013-06-28 at 11:04. Reason: addition

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •