andrew104 Posted December 3, 2002 Posted December 3, 2002 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 Quote
*Gurus* Derek Stone Posted December 3, 2002 *Gurus* Posted December 3, 2002 Dim sWords() As String = InputText.Text.Split(" "c) MessageBox.Show(sWords.GetUpperBound(0).ToString(), Application.ProductName) Quote Posting Guidelines
andrew104 Posted December 3, 2002 Author Posted December 3, 2002 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. Quote
andrew104 Posted December 3, 2002 Author Posted December 3, 2002 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. Quote
wyrd Posted December 3, 2002 Posted December 3, 2002 (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 December 3, 2002 by wyrd Quote Gamer extraordinaire. Programmer wannabe.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.