Page 1 of 1

String-Commands vs String-Functions

Posted: 15 Jan 2014, 08:25
by jNizM
StringCommands vs StringFunctions()* (*Prefers)

StringMid vs SubStr()

Code: Select all

InputVar := "AutoHotkey"

; StringMid, OutputVar, InputVar, StartChar [, Count , L]
StringMid, OutputVar, InputVar, 5, 3
MsgBox % OutputVar                                          ; Hot

; SubStr(String, StartingPos [, Length])
MsgBox % SubStr(InputVar, 5, 3)                             ; Hot
StringLeft vs SubStr()

Code: Select all

InputVar := "AutoHotkey"

; StringLeft, OutputVar, InputVar, Count
StringLeft, OutputVar, InputVar, 4
MsgBox % OutputVar                                          ; Auto

; SubStr(String, StartingPos [, Length])
MsgBox % SubStr(InputVar, 1, 4)                             ; Auto
StringRight vs SubStr()

Code: Select all

InputVar := "AutoHotkey"

; StringRight, OutputVar, InputVar, Count
StringRight, OutputVar, InputVar, 3
MsgBox % OutputVar                                          ; key

; SubStr(String, StartingPos [, Length])
MsgBox % SubStr(InputVar, -2)                               ; key
StringTrimLeft vs SubStr()

Code: Select all

InputVar := "AutoHotkey"

; StringTrimLeft, OutputVar, InputVar, Count
StringTrimLeft, OutputVar, InputVar, 4
MsgBox % OutputVar                                          ; Hotkey

; SubStr(String, StartingPos [, Length])
MsgBox % SubStr(InputVar, (4 + 1))                          ; Hotkey
StringTrimRight vs SubStr()

Code: Select all

InputVar := "AutoHotkey"

; StringTrimRight, OutputVar, InputVar, Count
StringTrimRight, OutputVar, InputVar, 3
MsgBox % OutputVar                                          ; AutoHot

; SubStr(String, StartingPos [, Length])
MsgBox % SubStr(InputVar, 1, -3)                            ; AutoHot
StringLen vs StrLen()

Code: Select all

InputVar := "AutoHotkey"

; StringLen, OutputVar, InputVar
StringLen, OutputVar, InputVar
MsgBox % OutputVar                                          ; 10

; StrLen(String)
MsgBox % StrLen(InputVar)                                   ; 10
StringGetPos vs InStr()

Code: Select all

InputVar := "AutoHotkey"

; StringGetPos, OutputVar, InputVar, SearchText [, L#|R#, Offset]
StringGetPos, OutputVar, InputVar, Hot
MsgBox % OutputVar                                          ; 4

; InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])
MsgBox % InStr(InputVar, "Hot")                             ; 5
IfInString vs if InStr()

Code: Select all

InputVar := "AutoHotkey"

; IfInString, var, SearchString
IfInString, InputVar, Hot
    MsgBox % "Hot is in AutoHotkey"

; InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])
if InStr(InputVar, "Hot")
    MsgBox % "Hot is in AutoHotkey"
IfNotInString vs if !InStr()

Code: Select all

InputVar := "AutoHotkey"

; IfNotInString, var, SearchString
IfNotInString, InputVar, Toy
    MsgBox % "Toy is not in AutoHotkey"

; InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])
if !(InStr(InputVar, "Toy"))
    MsgBox % "Toy is not in AutoHotkey"

Last Update: 2015-10-08

Re: String-Commands vs String-Functions

Posted: 15 Jan 2014, 09:40
by just me
StringTrimLeft vs SubStr()

Code: Select all

InputVar := "AutoHotkey"
Trim := 4

; StringTrimLeft, OutputVar, InputVar, Count
StringTrimLeft, OutputVar, InputVar, %Trim%
MsgBox, % OutputVar                                          ; Hotkey

; SubStr(String, StartingPos [, Length])
MsgBox, % SubStr(InputVar, Trim + 1)                         ; Hotkey
... should not rely on StrLen()

Re: String-Commands vs String-Functions

Posted: 15 Jan 2014, 14:08
by kon
? Should it be...
IfNotInString vs if !InStr()

Code: Select all

InputVar := "AutoHotkey"

; IfNotInString, var, SearchString
IfNotInString, InputVar, Toy
    MsgBox, Toy is not in AutoHotkey

; InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])
if !InStr(InputVar, "Toy")
    MsgBox, Toy is not in AutoHotkey
Edit: and add quotes around "Hot" in IfInString vs if InStr()

Re: String-Commands vs String-Functions

Posted: 08 Oct 2015, 09:34
by jNizM
Updated: StringTrimRight vs SubStr()

Re: String-Commands vs String-Functions

Posted: 08 Oct 2015, 14:54
by Soft
just suddenly curious,is Instr faster than IfInString?

Re: String-Commands vs String-Functions

Posted: 10 Jun 2016, 18:40
by jesushh
Hello, I came to this post looking for the difference between StrLen and StringLen.
I am not sure about the conclusion of your posts.

Is supposed to be better to use one of them, either commands or functions?
Or just showing how to do in both ways?

Re: String-Commands vs String-Functions

Posted: 13 Jun 2016, 01:41
by jNizM
To show new and old members how to use the functions instand of the commands or for the ones who want to move to v2 as soon it is final.

Removed commands:
- StringGetPos
- StringLeft
- StringLen
- StringMid
- StringRight
- StringTrimLeft
- StringTrimRight

Ref: v2-changes