String-Commands vs String-Functions

Helpful script writing tricks and HowTo's
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

String-Commands vs String-Functions

15 Jan 2014, 08:25

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
Last edited by jNizM on 15 Oct 2014, 04:54, edited 4 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
just me
Posts: 9783
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: String-Commands vs String-Functions

15 Jan 2014, 09:40

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()
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: String-Commands vs String-Functions

15 Jan 2014, 14:08

? 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()
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: String-Commands vs String-Functions

08 Oct 2015, 09:34

Updated: StringTrimRight vs SubStr()
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: String-Commands vs String-Functions

08 Oct 2015, 14:54

just suddenly curious,is Instr faster than IfInString?
AutoHotkey & AutoHotkey_H v1.1.22.07
jesushh
Posts: 8
Joined: 09 May 2016, 04:59
Location: Malaga / Krakow

Re: String-Commands vs String-Functions

10 Jun 2016, 18:40

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?
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: String-Commands vs String-Functions

13 Jun 2016, 01:41

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
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 5 guests