Is there a simple/fast way to detect if a string has mixed lower and upper case characters or not?
So:
“This is a string with Upper and Lower Case” -> should return true
“This is a string only has lowercase chars” -> should return false
“THIS IS A STRING WITH ONLY UPPERCASE CHARS” -> should return false
I was thinking checking for the ASCII code for each character. But maybe there is a simpler way?
Someone may have some thoughts regarding whether or not this a good idea in regards to encoding issues but this works as a quick and dirty solution:
[code] dim x as string
x = “this is A test.”
if StrComp(x, Uppercase(x), 0) = 0 then
MsgBox(“uppercase”)
elseif StrComp(x, Lowercase(x), 0) = 0 then
MsgBox(“lowercase”)
else
MsgBox(“mixed”)
end if[/code]
Scott’s solution is sound, but it begs the question, what about punctuation or numbers? Do they count?
This regular expression will emulate Scott’s solution, as an alternative:
(?s-i)\\p{Ll}.*\\p{Lu}|\\p{Lu}.*\\p{Ll}
A match means mixed case.
Yes, the strings can have punctuation and numbers. Does that affect Scott’s solution?
And thanks for the regex expression. I don’t use regex a lot because I struggle with making good/effective expressions. You seem to be a true wizard doing this.
I’m unaware of uppercase() and lowercase() changing punctuation but I don’t deal with a wide variety of languages or encodings. As such, I don’t know if that’s something that could theoretically occur.
Private Function isMixedCase(S as string) As Boolean
return (StrComp(s,s.Uppercase,0) <> 0 and StrComp(s,s.Lowercase,0) <> 0)
End Function