Sometimes I like to share quick (but hopefully readable) code without using custom functions.
Other issues might be two-way compatibility, to write something that can be used in an expression, a one-liner, to write a stand-alone function with no dependencies, to avoid 'Loop' because it's slow. Or I weigh up, slightly ugly/unreadable code with no dependencies, versus dependency. Or for a quick demo/tutorial script, trade-offs such as a few milliseconds drop in performance, for added script clarity or fewer lines.
Code: Select all
;'StrCount'
;count occurrences of 'ab'
MsgBox, % StrSplit("abcabcabc", "ab").Length() - 1
;count occurrences of 'abc'
MsgBox, % StrSplit("abcabcabc", "abc").Length() - 1
;count items
MsgBox, % StrSplit("abc,abc,abc", ",").Length()
;e.g. this technique is used here:
;combinations and permutations (and anagrams) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=34244&p=158874#p158874
;to use StrReplace and return a value within a single expression:
;note: division by 0 to give a blank string
vText := "abc,abc,abc"
MsgBox, % "item count: " StrReplace(vText,",","",vCount)/0 (vCount + 1)
;comparing the length of the string before and after:
vText := "abcabcabc"
MsgBox, % (StrLen(vText) - StrLen(StrReplace(vText, "abc"))) / 3
MsgBox, % StrLen(vText) - StrLen(StrReplace(vText, "abc", "__"))
vText := "abcabcabc"
vNeedle := "abc"
MsgBox, % (StrLen(vText) - StrLen(StrReplace(vText, vNeedle))) / StrLen(vNeedle)
MsgBox, % StrLen(vText) - StrLen(StrReplace(vText, vNeedle, SubStr(vNeedle, 2)))
;a theoretical StrCount function:
;MsgBox, % StrCount(vText, vNeedle)
;MsgBox, % StrCount(vText, vNeedle, vCaseSen)
;==============================
;if var in
;'if var in' (due to replaced) alternative, no RegEx escapement issues
MsgBox, % {aa:0,bb:0,cc:0}.HasKey("aa")
MsgBox, % {aa:0,bb:0,cc:0}.HasKey("dd")
MsgBox, % {aa:1,bb:1,cc:1}["aa"]
MsgBox, % {aa:1,bb:1,cc:1}["dd"]
a := "aa", b := "bb", c := "cc"
MsgBox, % {(a):0,(b):0,(c):0}.HasKey("aa")
MsgBox, % {(a):0,(b):0,(c):0}.HasKey("dd")
MsgBox, % Object("a",0,"b",0,"c",0).HasKey("a")
MsgBox, % Object("a",0,"b",0,"c",0).HasKey("d")
;MsgBox, % Object("a",0,"b",0,"c",0).HasKey("" vText)
MsgBox, % Object(StrSplit("a,,b,,c,")*).HasKey("a")
MsgBox, % Object(StrSplit("a,,b,,c,")*).HasKey("d")
;MsgBox, % Object(StrSplit("a,,b,,c,")*).HasKey("" vText)
;the same technique as here:
;convert list to simple array - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=33663
oArray := StrSplit("a,,b,,c,")
MsgBox, % Object(oArray*).HasKey("a")
MsgBox, % Object(oArray*).HasKey("d")
oArray := ""
;that method is better than this one, as no delimiter is needed
vText := "a"
MsgBox, % InStr("|a|b|c|", "|" vText "|")
vText := "d"
MsgBox, % InStr("|a|b|c|", "|" vText "|")
;==============================
;if var contains/starts/ends
;I think I need to use RegEx for if var contains/starts/ends
;==============================
;'StrRept'
;Replicate() : Repeats a string N times - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=33977
Code: Select all
q:: ;if var in/contains/starts/ends examples
vList := "abcde,abcdex,xabcde"
Loop, Parse, vList, % ","
{
vText := A_LoopField
MsgBox, % (vText ~= "^(abcde|fghij|klmno)$") ;in
MsgBox, % (vText ~= "bcd|ghi|lmn") ;contains
MsgBox, % (vText ~= "^(abc|fgh|klm)") ;contains (starts)
MsgBox, % (vText ~= "(cde|hij|mno)$") ;contains (ends)
}
return
w:: ;if var in/contains/starts/ends examples
vList := "abcde,abc,cde"
Loop, Parse, vList, % ","
{
;note: any special characters in vText would have to be escaped
vText := A_LoopField
MsgBox, % ("abcde" ~= "^" vText "$") ;in
MsgBox, % ("abcde" ~= vText) ;contains
MsgBox, % ("abcde" ~= "^" vText) ;contains (starts)
MsgBox, % ("abcde" ~= vText "$") ;contains (ends)
}
return
[EDIT:] Some updates:
Code: Select all
q:: ;string count
vText := "_abc_abc_abc_"
MsgBox, % StrLen(RegExReplace(vText, "(a)(?=bc)|.", "$1"))
MsgBox, % Format("", StrReplace(vText, "abc", "", vCount)) vCount
return
simplest way to make a RegEx needle literal? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30420