# Thread: How many special characters are there in a string

1. ## 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. 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. Thank you very much.

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

#### Posting Permissions

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