ChrsIn( Var, "==" or "<>", ChrsList )

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 347
Joined: 29 Sep 2013, 16:58

ChrsIn( Var, "==" or "<>", ChrsList )

20 Nov 2018, 11:45

ChrsIn() Validates Chrs in a Var with/against a list of valid/invalid chrs.

Function:

Code: Select all

ChrsIn( Var, C, ChrsList ) {                                    ; by SKAN on D1BK @ goo.gl/3wSZfF
Return ( DllCall( "Shlwapi\Str" (C="=="?"":"C") "Spn", "Str",Var,"Str",ChrsList ) = StrLen(Var) )
}
Usage Example:

Code: Select all

MsgBox % ChrsIn( "AABBCCDD", "==", "0xX1234546789AaBbCcDdEeFf" ) ; Validating HEX 
MsgBox % ChrsIn( "Skan.jpg", "<>", "\/:*?""<>|" )                ; Validating File name
MSDN Reference: StrSpn | StrCSpn | Shell String Handling Functions





Without DllCall:

Using Parse Loop:

Code: Select all

;== method

Str  := "0xAABBCC"
Chrs := "0xX1234546789AaBbCcDdEeFf"
IsValid := 1

Loop, Parse, Str,, %Chrs% ; Will never iterate when Str contains valid Chrs
{
    IsValid := 0
    Break
}

MsgBox % IsValid 


;<> method


Str  := "Skan.jpg"
Chrs := "\/:*?""<>|"

IsValid := 1

Loop, Parse, Str, %Chrs% ; will iterate only once when Str contains valid Chrs 
    If (A_Index=2)       ; atleast twice when invalid 
    {
      IsValid := 0
      Break
    }

MsgBox % IsValid 


Using StrSplit()

Code: Select all

;== method

IsValid := ! StrSplit( "0xAABBCC",, "0xX1234546789AaBbCcDdEeFf" ).Length()
MsgBox % IsValid 

;<> method

IsValid := StrSplit( "Skan.jpg", StrSplit("\/:*?""<>|")).Length() < 2
MsgBox % IsValid 

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot] and 45 guests