is there any function for StrSplit() from right?
something like this
Code: Select all
input = E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png
output := StrSplitRight(input ,\)
msgbox, % output[2] ; result : Sub Folder4\picture.png
Code: Select all
input = E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png
output := StrSplitRight(input ,\)
msgbox, % output[2] ; result : Sub Folder4\picture.png
Code: Select all
input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
MsgBox % StrSplitRight(input,"\",2)
StrSplitRight(string,delim,count) {
for i in Arr := StrSplit(string,delim) {
output := Arr.pop() . (A_Index > 1 ? delim . output : "")
} until (A_Index = Count)
return output
}
Code: Select all
input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
output := StrSplit(input, "\")
msgbox % output[output.Length()-1] ; Sub Folder 4
Code: Select all
;StrSplit MaxParts (v1.1.28+)
MsgBox, % StrSplit("a,b,c,d,e", ",",, 3)[3] ;c,d,e
Code: Select all
q:: ;get the last n array items, joined, via StrSplit MaxParts (v1.1.28+)
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
StrReplace(vPath, "\",, vCount)
Loop, % vCount+1
{
vNum := A_Index
vOutput .= vNum "`t" StrSplit(vPath, "\",, vCount+2-vNum)[vCount+2-vNum] "`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return
this is what i'm looking for...thank you very muchXtra wrote:It can be done with regex but it is generally slower for simple string manipulation.
Example function:Code: Select all
input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png" MsgBox % StrSplitRight(input,"\",2) StrSplitRight(string,delim,count) { for i in Arr := StrSplit(string,delim) { output := Arr.pop() . (A_Index > 1 ? delim . output : "") } until (A_Index = Count) return output }
Example,For loop wrote:Existing key-value pairs may be modified during the loop, but inserting or removing keys may cause some items to be skipped or enumerated multiple times
Code: Select all
for k in a:=[1,2,3,4,5,6]
str .= a.pop() "`n"
msgbox % str
Code: Select all
StrSplitRight(string, delim, count){
return substr(string, instr(string, delim,, -1, count) + 1)
}
Code: Select all
q:: ;use SubStr to get the last n parts of a string
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
vNum := 2
vIsV1 := !!InStr(1,1,1,0), vPos := vIsV1 ? -1 : 0
vPos := InStr(vPath, "\", 0, vPos, vNum)
MsgBox, % SubStr(vPath, vPos+1)
return
w:: ;use SubStr to get the last n parts of a string
vPath := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
StrReplace(vPath, "\",, vCount)
vIsV1 := !!InStr(1,1,1,0)
Loop, % vCount+1
{
vNum := A_Index
vPos := vIsV1 ? -1 : 0
vPos := InStr(vPath, "\", 0, vPos, vNum)
vOutput .= vNum "`t" SubStr(vPath, vPos+1) "`r`n"
}
Clipboard := vOutput
MsgBox, % vOutput
return
Code: Select all
input := "E:\Folder 1\Sub Folder 2\Sub Folder 3\Sub Folder 4\picture.png"
MsgBox % StrSplitRight(input,"\",2)
StrSplitRight(string,delim,count) {
Arr := StrSplit(string,delim)
Loop % Arr.length() {
output := Arr.pop() . (A_Index > 1 ? delim . output : "")
} until (A_Index = Count)
return output
}
I have a little question about your simpler version,Helgef wrote: A simpler version,Cheers.Code: Select all
StrSplitRight(string, delim, count){ return substr(string, instr(string, "\",, -1, count) + 1) }
Code: Select all
StrSplitRight(string, count){
return substr(string, instr(string, "\",, -1, count) + 1)