0

Say I have a string for the full path of a directory

let myPath = '/go/src/github.com/MyDomain/MyProject'

I would like to have an array like

['go', 'src', 'github.com', 'MyDomain', 'MyProject']

What is the best way of doing that ? I would prefer to make this platform agnostic. So if there is a split() function tailored for paths, I would prefer that.

Otherwise I think

let out = split(myPath, '/') 

Should do the trick ?

  • Be aware of the UNC pathes (\\server\share\path\to\file) on Windows. – Ralf Jun 8 at 6:37
1

I would use regular split function with regex covering 2 path separators.

Windows can use / or \, plus mix of both:

echo split('/go/src/github.com/MyDomain/MyProject', '[/\\]')
echo split('C:\go/src/github.com/MyDomain/MyProject', '[/\\]')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.