Detecting mixed case strings in ASP Classic

I needed to detect a mixed case string in classic ASP. I define a mixed case string as containing both upper and lower case characters, like AuxagGsrLpa. I could not find a free function on the web to make this decision, so I wrote one.

<%
	Function isMixedCase( str )
		'detects mixed case strings using the english alphabet
		'ignores spaces and other non-alphabetic characters
		str = trim( str )
		isMixed = false
		lastCase = ""
		for i=1 to len( str )
			currentChar = mid( str, i, 1 )
			if asc( currentChar ) >= 65 and asc( currentChar ) <= 90 then
				if lastCase <> "" and lastCase <> "upper" then
					isMixed = true
					exit for
				else
					lastCase = "upper"
				end if
			else
				if asc( currentChar ) >= 97 and asc( currentChar ) <= 122 then
					'lower
					if lastCase <> "" and lastCase <> "lower" then
						isMixed = true
						exit for
					else
						lastCase = "lower"
					end if
				else
					lastCase = ""
				end if
			end if
		next
		if isMixed then
			isMixedCase = true
		else
			isMixedCase = false
		end if
	End Function
%>

Here are a few test strings to demonstrate the functionality:


<% response.write "Hello " & isMixedCase( "Hello " ) & " <br>" response.write "hello! " & isMixedCase( "hello!" ) & " <br>" response.write "HELLO " & isMixedCase( "HELLO " ) & " <br>" response.write "hello there " & isMixedCase( "hello there" ) & " <br>" response.write "hellothere " & isMixedCase( "hellothere" ) & " <br>" response.write "hellotherE " & isMixedCase( "hellotherE" ) & " <br>" %>

Here is the resulting HTML output of the tests:

Hello True
hello! False
HELLO False
hello there False
hellothere False
hellotherE True

2 comments

  1. Neat!
    But I don’t think you need to iterate over each character in the string…
    there are built in functions you can use, like lCase and uCase. lCase makes everything in a string lower case, and uCase makes everything upper case.
    You can use that in a test, to see if a string is either only in lower case or only in upper case… which means the string has no mixed case chars. and then the function should return false.
    Else there is only one possibility left, and that is the string contains both lower case and upper case chars, and the function should return true.


    Function isMixedCase( strIn)
    If strIn = lCase(strIn) or strIn = uCase(strin)
    isMixedCase = False
    Else
    isMixedCase = true
    End If
    End Function

    It should be possible to make that code smaller (sacriifising readability), I do that some times


    Function isMixedCase( strIn )
    isMixedCase = Not( ( strIn = lCase(strIn) ) or ( strIn = uCase(strIn) ) )
    End Function

Leave a comment

Your email address will not be published. Required fields are marked *