counting the number of occurrences of a characher in a string

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: counting the number of occurrences of a characher in a string

1. Elmosca Guest

## counting the number of occurrences of a characher in a string

Newbie question: Anyone knows how to count the number of occurrences of a
characher in a string (eg the result of counting the number of "A" in
"EEOOAA" should be 2), TIA

Regards,

2. Paul Clement Guest

## Re: counting the number of occurrences of a characher in a string

On Thu, 10 Oct 2002 14:11:16 +0200, "Elmosca" <elmosca@terra.es> wrote:

¤ Newbie question: Anyone knows how to count the number of occurrences of a
¤ characher in a string (eg the result of counting the number of "A" in
¤ "EEOOAA" should be 2), TIA

You could simply call the Instr function until it returns a value of zero.

Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)

3. Elmosca Guest

## Re: counting the number of occurrences of a characher in a string

Sorry, but I don't know how to do it :-( ...

"Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com> escribió en el
mensaje news:uvraqu8lvpph8f8o7b3h12v5o0b8vhb9ti@4ax.com...
> On Thu, 10 Oct 2002 14:11:16 +0200, "Elmosca" <elmosca@terra.es> wrote:
>
> ¤ Newbie question: Anyone knows how to count the number of occurrences of

a
> ¤ characher in a string (eg the result of counting the number of "A" in
> ¤ "EEOOAA" should be 2), TIA
>
> You could simply call the Instr function until it returns a value of zero.
>
>
> Paul ~~~ pclement@ameritech.net
> Microsoft MVP (Visual Basic)

4. Phil Weber Guest

## Re: counting the number of occurrences of a characher in a string

> Sorry, but I don't know how to do it :-( ...

Mosca:

Dim Occurrences As Integer
Dim StringToCheck As String = "EEOOAA"
Dim StringToFind As String = "A"

Dim Start As Integer
Dim Found As Integer
Do
Start = Found + 1
Found = InStr(Start, StringToCheck, StringToFind)
If Found = 0 Then Exit Do
Occurrences += 1
Loop

--
Phil Weber

5. Elmosca Guest

## Re: counting the number of occurrences of a characher in a string

Thanks Phil! I am begining with vb.net as I am a web designer who has jumped
to the .NET Framework. Now I'm learning the language to improve my webs :-)

"Phil Weber" <pweber@nospam.fawcette.com> escribió en el mensaje
news:3da578d6\$1@10.1.10.29...
> > Sorry, but I don't know how to do it :-( ...

>
> Mosca:
>
> Dim Occurrences As Integer
> Dim StringToCheck As String = "EEOOAA"
> Dim StringToFind As String = "A"
>
> Dim Start As Integer
> Dim Found As Integer
> Do
> Start = Found + 1
> Found = InStr(Start, StringToCheck, StringToFind)
> If Found = 0 Then Exit Do
> Occurrences += 1
> Loop
>
> --
> Phil Weber
>
>

6. Tom Shelton Guest

## Re: counting the number of occurrences of a characher in a string

"Elmosca" <elmosca@terra.es> wrote in message news:3da5681f@10.1.10.29...
> Newbie question: Anyone knows how to count the number of occurrences of a
> characher in a string (eg the result of counting the number of "A" in
> "EEOOAA" should be 2), TIA
>
> Regards,

You already got a very good answer, but there is an alternative. You can
use the string objects IndexOf method to accomplish the same thing. I
believe it will be slightly faster, since I'm sure InStr is just calling
IndexOf internally. Anyway, here is the revised method using IndexOf - the
code is just about the same as Phil's:

Dim str As String = "EEOOAA"
Dim index As Integer = 0
Dim found As Integer = 0

Do
index = str.IndexOf("A"c, index)
If index = -1 Then Exit Do
index += 1
found += 1
Loop

Console.WriteLine(found)

7. Phil Weber Guest

## Re: counting the number of occurrences of a characher in a string

> I am begining with vb.net...

Mosca: Since you're a beginner ;-), here's another way to count the occurrences
of a substring within a string:

Imports System.Text.RegularExpressions

Dim StringToCheck As String = "EEOOAA"
Dim StringToFind As String = "A"
Dim exp As New Regex(StringToFind, RegexOptions.IgnoreCase)
Dim Occurrences As Integer = exp.Matches(StringToCheck).Count

--
Phil Weber

8. Elmosca Guest

## Re: counting the number of occurrences of a characher in a string

Thanks Phil, this is a clean solution

"Phil Weber" <pweber@nospam.fawcette.com> escribió en el mensaje
news:3da6a2c6\$1@10.1.10.29...
> > I am begining with vb.net...

>
> Mosca: Since you're a beginner ;-), here's another way to count the

occurrences
> of a substring within a string:
>
> Imports System.Text.RegularExpressions
>
> Dim StringToCheck As String = "EEOOAA"
> Dim StringToFind As String = "A"
> Dim exp As New Regex(StringToFind, RegexOptions.IgnoreCase)
> Dim Occurrences As Integer = exp.Matches(StringToCheck).Count
>

help.
> --
> Phil Weber
>
>

9. Registered User
Join Date
Nov 2008
Posts
1
This is a great solution...however in my project I had to search for vbCr - Chr(13) and for this option is does not work very good..so I use the IndexOf option and it worked great!

Many thanks all for the great knowledge!

Originally Posted by Phil Weber
> I am begining with vb.net...

Mosca: Since you're a beginner ;-), here's another way to count the occurrences
of a substring within a string:

Imports System.Text.RegularExpressions

Dim StringToCheck As String = "EEOOAA"
Dim StringToFind As String = "A"
Dim exp As New Regex(StringToFind, RegexOptions.IgnoreCase)
Dim Occurrences As Integer = exp.Matches(StringToCheck).Count

--
Phil Weber

10. Post the code that you used. You never know....6 years from now someone might find what you post and it will help them.

11. Registered User
Join Date
May 2011
Posts
1
I know this is an old, old topic, but it could also be done this way:

Code:
```Dim StringToCheck As String = "EEOOAA"
Dim StringToFind As String = "A"
Dim NewStr as String = StringToCheck.Replace(StringToFind, "")

Dim Occurrences As Integer = StringToCheck.Length - NewStr.Length
Console.WriteLine(Occurrences.ToString)```
It'll work with VbCr, too:
Code:
```Dim StringToCheck As String = "EEOOAA" & vbCr & "EEOOAA" & vbCr & "EEOOAA"
Dim StringToFind As String = vbCr
Dim NewStr As String = StringToCheck.Replace(StringToFind, "")

Dim Occurrences As Integer = StringToCheck.Length - NewStr.Length
Console.WriteLine(Occurrences.ToString)```

12. Senior Member
Join Date
Aug 2004
Location
Orange, California
Posts
1,263
Thought I would throw one in as well.
Code:
```  Function CharCount(ByVal str As String, ByVal chr As Char) As Integer
For I As Long = 1 To str.Length
If GetChar(str, I) = chr Then CharCount += 1
Next
End Function```
Last edited by Ron Weller; 05-07-2011 at 05:24 PM.

13. Senior Member
Join Date
Aug 2004
Location
Orange, California
Posts
1,263
Another interesting variation which uses the replace() function to first remove all occurances of the substring, and then calculates the number of occurances by taking the overall change in length divided by the length of the substring.

Code:
```  Function SubstringCount(ByVal search As String, ByVal find As String) As Integer
SubstringCount = (search.Length - Replace(search, find, "").Length) / find.Length
End Function```

14. Registered User
Join Date
Aug 2012
Posts
1
Originally Posted by vbmaniac
This is a great solution...however in my project I had to search for vbCr - Chr(13) and for this option is does not work very good..so I use the IndexOf option and it worked great!

Many thanks all for the great knowledge!
You may need to escape the character, I was searching for pipes (|) and had to do a regex.escape. See http://msdn.microsoft.com/en-us/libr...ex.escape.aspx

15. Registered User
Join Date
Feb 2010
Location
Central WV, USA
Posts
213
Here is another Solution, somtimes its handy to put code that may be used multiple times in a global function

Code:
```Public Function ReturnCharCount(ByVal myString As String, ByVal myLookupChar As String) As Integer
'Returns the Character Occurance Count for the Supplied String
Dim myCount As Integer = 0

For Each C As Char In myString
If C.ToString = myLookupChar Then myCount += 1
Next

Return myCount
End Function```
you could then place this function into a module and be able to access it anywhere in your program

To use the function simply call it from any form...
Code:
```        Dim myString As String = InputBox("Enter Master String")
Dim myLookupChar As String = InputBox("Enter the Char to Count")
Dim myCount As Integer = 0

myCount = ReturnCharCount(myString, myLookupChar)  ' Function Called Here
MsgBox("Count is " & myCount)```
Last edited by rrjii2000; 08-30-2012 at 01:21 PM.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise