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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A method "isUpperCase" ? 1

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
US
I need to check a string to see if all of its characters are uppercase.

Is there a VB method to do this?

Thanks,
RebLazer :)
 
You could use something like this

Private Sub Command1_Click()
Dim bUpper As Boolean
Dim str As String

str = Text1.Text
bUpper = InStr(1, str, UCase$(str), vbBinaryCompare)
Command1.Caption = bUpper
End Sub

Let me know if this helps. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
or if you prefer a function

Private Sub Command1_Click()
Command1.Caption = UpperCase(Text1.Text)
End Sub

Private Function UpperCase(ByVal str As String) As Boolean
UpperCase = InStr(1, str, UCase$(str), vbBinaryCompare)
End Function If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
Hm I don't think so, but very easy to write one. All you need to do is to convert the string to upper case and compare it to the original using strComp, ensuring you select the vbBinaryCompare option. (the vbTextCompare compares the text ignoring case, which you don't want!)

That should be enough to help you, but let me know if you need more....
 
RebLazer,

I couldn't find a vb function, but you could create your own. You can start with the following:

Public Function isUppercase(varString As String) As Boolean
Dim bTemp As Boolean
Dim sTemp As String 'This holds the character that we are
'currently checking.
bTemp = True
Dim iLoop As Integer
For iLoop = 1 To Len(varString)
sTemp = Mid(varString, iLoop, 1)
If Asc(sTemp) < 65 Or Asc(sTemp) > 90 Then
bTemp = False
Exit Function
End If
Next
isUppercase = bTemp
End Function
 
cdukes - your function should work provided that all of the characters in the string are in fact letters. But if you have punctuation marks and/or digits, then your function will fail.

That being said, if the question is in fact, Are all letters within the string uppercase? then your function will fail. But if the real question is Does the string only contain uppercase letters?, then your function should be ok.

What I don't understand, if why can't you simply do the following statement:

Dim IsAllUpper as Boolean
IsAllUpper = (UCase(str) = str)
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top