Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have to count specific letters entered in a text box (which I've completeed, as well as count the total number of words. I am going to call anything with a space before and after it a word. I'm not really sure what to do for this. If someone has an idea it would be greatly appreciated! Thank you.

 

Here is my code to count a character...

 

   ' Count the number of times that character a is used in the string InputText
   Function strCountA1(ByVal InputText As String, ByVal a As Char) As Integer
       Dim i As Integer
       For i = 0 To InputText.Length - 1
           If InputText.Substring(i, 1) = a Then
               strCountA1 = strCountA1 + 1
           End If
       Next
   End Function

Posted
Thanks! How can I display the total in a label instead of a message box? Also, it seems to just count spaces, can I make it a little more selective and require some characters in between spaces? Thank you.
Posted

Ok regarding my first question about displaying it in a label, I figured it out.

 

       Me.WordCountLabel.Text = (sWords.GetUpperBound(0).ToString())

 

However, my question about making it more selective still stands. It is better than what I had before, but basically just counts spaces. Thanks.

Posted (edited)

Regular expressions are the easiest and best way if you want an accurate count of words in a String..

 

Dim r As New System.Text.RegularExpressions.Regex("\w+")
WordCountLabel.Text = r.Matches(InputText).Count

 

You can easily count how many times a is in a String as well...

 

Dim r As New System.Text.RegularExpressions.Regex("[a]{1}")
strCountA1 = r.Matches(InputText).Count

Edited by wyrd
Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...