?
Solved

Newbie Question: Count the number of occurences of a character in a textbox

Posted on 2003-02-23
9
Medium Priority
?
135 Views
Last Modified: 2010-04-07
Hi
I need to find out how to count the number of occurences of a character (say, 1) in a textbox, and save the number to a variable. Is there a quick easy way of doing this?

Thanks
Noddingdog
0
Comment
Question by:Noddingdog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 8003187
Hello Noddingdog,

you could try

Dim i as integer
i=Len(Textbox1.Text)

HAGD:O)Bruintje
0
 

Author Comment

by:Noddingdog
ID: 8003207
Hi
I haven't come across the Len command before, what does it do?

ND
0
 
LVL 2

Expert Comment

by:Mennovdh
ID: 8003226
Private Function CountOccurences(Text As String, searchChar As String) As Long
Dim loc As Long
   
    loc = InStr(Text, searchChar)
    CountOccurences = 0
   
    Do While loc > 0
        loc = InStr(loc + 1, Text, searchChar)
        CountOccurences = CountOccurences + 1
    Loop
   
End Function

I have no idea if this is the best way to do it, though.

len() will give you the length of a string.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8003229
you can use this:

Private Sub Command1_Click()
    MsgBox CountChars("This is a test!", "s")
End Sub

Private Function CountChars(ByVal pstrSource As String, ByVal pstrSearchChar As String) As Integer
Dim intI As Integer
Dim intTotal As Integer

    For intI = 1 To Len(pstrSource)
        If Mid$(pstrSource, intI, 1) = pstrSearchChar Then
            intTotal = intTotal + 1
        End If
    Next intI
    CountChars = intTotal
End Function
0
 
LVL 44

Expert Comment

by:bruintje
ID: 8003239
hmmmm misread the q
0
 
LVL 5

Accepted Solution

by:
Rhaedes earned 300 total points
ID: 8003254
Try this one. It compares substracts the length of the string with all the characters you are searching for removed, with the length of the original string.

Private Function countChars(myString As String, myChar As String)
countChars = Len(myString) - Len(Replace(myString, myChar, ""))
End Function

Kindest regards,
Rhaedes
0
 

Author Comment

by:Noddingdog
ID: 8003302
Hi,
Thanks for all your help. Rhaedes seems to have a short answer that has proved useful, but is there any way to store the results of that function to a variable?

ND
0
 

Author Comment

by:Noddingdog
ID: 8003327
Sorry, penny just dropped. I know where I was going wrong >_<

Thanks again,
Noddingdog
0
 

Author Comment

by:Noddingdog
ID: 8003328
Thanks - good answer.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question