-
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,
-
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)
-
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)
-
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
-
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
>
>
-
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)
-
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
For more information, read about "Regular Expressions" in VB.NET's online help.
--
Phil Weber
-
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
>
> For more information, read about "Regular Expressions" in VB.NET's online
help.
> --
> Phil Weber
>
>
-
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
For more information, read about "Regular Expressions" in VB.NET's online help.
--
Phil Weber
-
Post the code that you used. You never know....6 years from now someone might find what you post and it will help them.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
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)
-
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.
-
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
-
 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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks