• How many special characters are there in a string

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » How many special characters are there in a string

    Author
    Topic
    #489826

    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”

    Viewing 1 reply thread
    Author
    Replies
    • #1398436

      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).

      • #1398437

        Thank you very much.

        • #1398851

          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
    • #1398857

      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.

    Viewing 1 reply thread
    Reply To: How many special characters are there in a string

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: