Select case that is case sensitive

Sorry for the confusing title . . .

I’d like to do a select case that is case sensitive: case “A” is not the same as case “a” nor is case “BOB” the same as case “bob”.

Any suggestions ?

Use StrComp to do a case-sensitive string comparison

http://documentation.xojo.com/index.php/StrComp

Btw that’s mentioned when you look at the = operator in the language reference :wink:

The syntax would look like

select case 0
case strcomp(x, "A", 0)
   ...
case strcomp(x, "a", 0)
   ...
case strcomp(x, "BOB", 0)
   ...
etc.

Tim . . . You meant “select case x” right?

No. As written, it will reach the first StrComp that evaluates to zero (equal).

This allows more choice than simply three :

[code] dim s as string = Textfield1.Text

Select case EncodeBase64(s)

case EncodeBase64(“A”)
msgbox “A”

case EncodeBase64(“a”)
msgbox “a”

end select[/code]

I tested the method aboce, and it does discriminate very nicely between A and a. However, as the result of encodeBase64 can itself be in both upper and lowercase, it is theoretically possible that there be confusion. Probably infinitesimal, though.

For single letters, one can also do :

[code] dim s as string = Textfield1.Text

Select case ASC(s)

case ASC(“A”)
msgbox “A”

case ASC(“a”)
msgbox “a”

end select[/code]

The same method could be made with longer strings with an additional string to hex method.

[code] dim s as string = Textfield1.Text

Select case String2Hex(s)

case String2Hex(“AAAA”)
msgbox “AAAA”

case String2Hex(“aaaa”)
msgbox “aaaa”

case String2Hex(“Ohlala”)
msgbox “Ohlala”

end select[/code]

[code]Function String2Hex(S as string) As String
if s = “” then return s

dim Result as string

for i as integer = 0 to len(s)
Result = Result + Hex(asc(mid(s,i,1)))
next

return Result
End Function
[/code]

But why?

There are several ways to do things…

[code]Dim mb As MemoryBlock = “a”

Select Case mb
Case “A”

Case “a”

Case “BOB”

End[/code]

String2Hex is not needed as EncodeHex is part of Xojo.

There is always something to learn. Thank you Christian.

try it:

MsgBox EncodeBase64("AAa")+EndOfLine+EncodeBase64("AAG")

And “QUFh” and “QUFH” are the considered equal case insensitive.

select case true
case strcomp(x, "A", 0) = 0 // it either matches exactly or not
   ...
case strcomp(x, "a", 0) = 0
   ...
case strcomp(x, "BOB", 0) = 0
   ...
case x.len() = 9

etc.

write as many cases as you want with whatever conditions you want
see the last one :slight_smile:
the first one that matches will be the case that is selected

The advantage to Norman’s version is that you can mix functions.

select case true
case StrComp( x, "A", 0) = 0
   ...
case IsNumeric( x )
  ...

you mean like i did :stuck_out_tongue:
it is a handy although not immediately obvious usage of select case

Be nice, I’m still half-asleep.

oh cmon … yer at least 2 hours ahead of me … its late there already :stuck_out_tongue:

Your solution was certainly the easiest to implement. I had already written the select case statement, but suddenly encountered a situation where I needed it to be case sensitive and this really helped.

Wouldn’t that fail with composed vs uncomposed characters?