- Btw, do make any suggestions re. whether it should be called StrPad or something else. Essentially the idea is to insert a string every n characters.
Code: Select all
;e.g.
;MsgBox, % JEE_StrHatch("abcdefghij", ",") ;a,b,c,d,e,f,g,h,i,j
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3) ;abc,def,ghi,j
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3, "R") ;a,bcd,efg,hij
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3, "O")[2] ;def
;vSep: separator
;vBlockLen: length of character blocks
;vOpt: R: from which end, start from right
;vOpt: O: return object (vSep is ignored)
;JEE_StrSlice
JEE_StrHatch(vText, vSep, vBlockLen:=1, vOpt:="")
{
if (vText = "")
return
if InStr(vOpt, "R")
vOffset := vBlockLen - Mod(StrLen(vText), vBlockLen)
else
vOffset := 0
if InStr(vOpt, "O")
{
oArray := []
oArray.SetCapacity(StrLen(vText)//vBlockLen)
vTemp := ""
Loop, Parse, vText
{
if (Mod(A_Index + vOffset, vBlockLen) = 0)
oArray.Push(vTemp A_LoopField), vTemp := ""
else
vTemp .= A_LoopField
}
if !(vTemp = "")
oArray.Push(vTemp)
return oArray
}
VarSetCapacity(vOutput, StrLen(vText)*2+(StrLen(vText)//vBlockLen)*StrLen(vSep)*2)
Loop, Parse, vText
{
if (Mod(A_Index + vOffset, vBlockLen) = 0)
vOutput .= A_LoopField vSep, vDoCrop := 1
else
vOutput .= A_LoopField, vDoCrop := 0
}
if vDoCrop
return SubStr(vOutput, 1, -StrLen(vSep))
else
return vOutput
}
[EDIT:] See a more appropriately named 'StrPad' function, lower down:
slice string, pad characters - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 05#p237105