String-Commands vs String-Functions

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

String-Commands vs String-Functions

15 Jan 2014, 08:25

StringCommands vs StringFunctions()* (*Prefers)

StringMid vs SubStr()

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

InputVar := "AutoHotkey"

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

; StrLen(String)
MsgBox % StrLen(InputVar) ; 10

StringGetPos vs InStr()

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
just me
Posts: 5396
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] [Download] GeSHi © Codebox Plus

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: 1760
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] [Download] GeSHi © Codebox Plus

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: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: String-Commands vs String-Functions

08 Oct 2015, 09:34

Updated: StringTrimRight vs SubStr()
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
GitHub: visionary1
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: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 5 guests