Jump to content

Making string checks case-sensitive


Recommended Posts

See topic title

Any way to do this?

Example:

$Choice = MsgBox(4,"Upper / Lower", "Press YES for upper case :: NO for lower case")
If $Choice = 6 Then
 $Letter = "A"
Else
 $Letter = "a"
EndIf

If $letter = "a" then ;Unfortunately, this is true, regardless of your choice.
 MsgBox(0,"You picked " & $Letter ,"The condition ( $Letter = ""a"" ) is met!")
EndIf

:(

Switch the operand in your comparison from "=" to "=="
Link to comment
Share on other sites

There are likley other methods to go about it, but the first that came to mind is:

Switch Asc($Letter)
 Case Asc("a")
 MsgBox(0,"lower case", "a")

 Case Asc("A")
 MsgBox(0,"upper case", "A")
EndSwitch

Switch Asc($Letter)
 Case 97
 MsgBox(0,"lower case", "a")

 Case 65
 MsgBox(0,"upper case", "A")
EndSwitch

The second switch, using the actual ascii character values (rather than converting them for each compare) would result in a lot less CPU usage.

Edit: The ASCII table is in the appendix of the helpfile.

Edit2: Another way to skin your cat (I'm sure there are more...):

If StringIsLower($Letter) Then
    Switch $Letter
        Case "a"
          MsgBox(0,"lower case", "a")
        Case "b"
    EndSwitch
Else
    Switch $Letter
        Case "A"
          MsgBox(0,"UPPER case", "A")
        Case "B"
    EndSwitch
EndIf

Edit3: Or... (although you might want a StringIsAlpha() test in there first):

If Asc($Letter) > 90 Then
    MsgBox(0,"lower case", $Letter)
Else
    MsgBox(0,"UPPER case", $Letter)
EndIf
Edited by Spiff59
Link to comment
Share on other sites

Thanks, used StringIsLower($Letter) , however this is a thing I had actually expected to have an Opt() for :(

Edit: Has this topic unleashed your inner letter checker, Spiff59 :)?

Edited by nf67
Link to comment
Share on other sites

See topic title

Any way to do this?

Example:

$Choice = MsgBox(4,"Upper / Lower", "Press YES for upper case :: NO for lower case")
If $Choice = 6 Then
 $Letter = "A"
Else
 $Letter = "a"
EndIf

If $letter = "a" then ;Unfortunately, this is true, regardless of your choice.
 MsgBox(0,"You picked " & $Letter ,"The condition ( $Letter = ""a"" ) is met!")
EndIf

:(

Hi, your script works perfectly as is!!! I'm guessing you think its not working becase the "text" in the MsgBox(flag, title, text) displays the little a regardless if YES or NO button is pressed.

Simply make the "text" section use the $Letter variable like you did in the "title" section of the MsgBox(). For example it should read:

MsgBox(0,'You picked ' & $Letter, 'The condition ( $Letter = "' & $Letter & '" ) is met!')

Note the use of single quotes makes it easier to read especially when wanting to include double quotes as part of the string.

Hope this helps!

Link to comment
Share on other sites

Sorry Ssubirias, but that's not what I mean.

The example I provided isn't case-sensitive; despite the fact that the script checks for "a", it also runs the code when "A" is found.

Thanks anyway :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...