StrCmp() : for case-sensitive string comparison

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

StrCmp() : for case-sensitive string comparison

30 Sep 2019, 08:51

Code: Select all

StrCmp(Str1, Op, Str2, CS:=1) { ; requires AutoHotkey v1.1.31+
Local R, SCS := A_StringCaseSense
  StringCaseSense, % (CS := !!CS)
  Switch Op {
    Case ">"        : R := (Str1 >  Str2) 
    Case ">="       : R := (Str1 >= Str2)
    Case "<"        : R := (Str1 <  Str2)
    Case "<="       : R := (Str1 <= Str2)
    Case "=="       : R := (Str1 == Str2)
    Case "!=","<>"  : R := (Str1 != Str2)
    default         : R := (CS ? (Str1 == Str2) : (Str1 = Str2))             
  } 
  StringCaseSense, %SCS%
Return R
}

#NoEnv
#Warn
#SingleInstance, Force

MsgBox % ("skan" != "SKAN")          ; False
MsgBox % StrCmp("skan","!=","SKAN")  ; True
My Scripts and Functions: V1  V2
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: StrCmp() : for case-sensitive string comparison

30 Sep 2019, 17:04

@SKAN, thanks for this, it will be useful for sure. Also a nice and thoughtful demonstration of the Switch function, which I didn't even know was in 1.1.31.00.
Regards,
burque505
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: StrCmp() : for case-sensitive string comparison

23 Sep 2021, 03:28

Thanks but can't we just write it like this?

Code: Select all

MsgBox % !("skan" == "SKAN")

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 267 guests