Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Upper Case Letters--HELP - URGENT

Status
Not open for further replies.
Joined
Apr 18, 2002
Messages
185
Location
US
I need help! I need to be able to read a string entered into a text that is at least 15 character long and count the number of Upper Case letters in that string... How do I do this???? Please help
 
Have a look at the Match Collection of RegularExpressions.

The Pattern would simply be "[A-Z]" and then you would just need the Count (or Length (I can't remember which)) property.


Hope this helps.

[vampire][bat]
 
Code:
Private Function NumberOfUppercaseCharacters(text As String) As Integer
  Dim c() As Char = text.ToCharArray
  Dim count As Integer

  For Each c2 As Char In c
    If Char.IsUpper(c2) Then
      count += 1
    End If
  Next
  Return count
End Sub

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
OK.. so this is what I have done so far...
Private Function NumberOfUppercaseCharacters(ByVal text As String) As Integer

End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TextBox1() As Char = Text.ToCharArray
Dim count As Integer

For Each c2 As Char In TextBox1
If Char.IsUpper(c2) Then
count += 1
End If
Next

End Sub

Now do I need to create a message box or another textbox so that I can display the results of that count - and where does that statment go - after the "Next" statement???

THANKS
 
Yes, after the Next statement, the count variable holds the number of uppercase characters. You can then use MsgBox(count) to show a messagebox, for example.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Ok... one more problem... I can display the count but no matter what I put in the text box it only gives me the count of 1 -- what am I doing wrong??

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TextBox1() As Char = Text.ToCharArray
Dim count As Integer

For Each c2 As Char In TextBox1
If Char.IsUpper(c2) Then
count += 1
End If
Next
MsgBox(count)
End Sub
 
you should probably change the code back to:

Dim c() As Char = TextBox1.Text.ToCharArray
Dim count As Integer

For Each c2 As Char In c
If Char.IsUpper(c2) Then
count += 1
End If
Next

MsgBox(count)


Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
THANK YOU SOOO MUCH FOR YOU HELP!!!! I know it was all in caps - but this fixed so many problems right now!

Happy Thanksgiving!
 
Ooops...One other question... What would be the best way to put error checking in this?? So that if there were less than 15 characters entered in the textbox - to give an error message??
 
Make these the first lines in the procedure:

Code:
If TextBox1.Text.Length < 15 Then
  MsgBox("Invalid format! Less than 15 characters.", MsgBoxStyle.Critical)
  Exit Sub
End If

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Using Regular Expressions is so much easier:


Code:
 Private Function GetCaps(ByVal source As String, ByVal requirednumber As Integer) As Boolean

    Dim rx As New System.Text.RegularExpressions.Regex("[A-Z]")
    Return rx.Matches(source).Count = requirednumber

  End Function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim s As String = "This has some CAPS"
    MessageBox.Show(GetCaps(s, 15).ToString)

  End Sub

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top