NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 14 of 30 · Data, functions and the command lineCut a line into fields

split cuts at every separator, token skips empty parts.

Programming on the NAS › BASIC in 30 lessons

14-split.yab Download 14-split.yab

rem split cuts a line at every separator; token skips empty parts
dim part$(1)
row$ = "3,Example HDD B,12000,53"

n = split(row$, part$(), ",")
print n, " parts"
for i = 1 to n
  print i, ": ", part$(i)
next i

n = token("INFO   backup   finished", part$(), " ")
print n, " words, the last one is ", part$(n)

Terminal

$ yabasic 14-split.yab
4 parts
1: 3
2: Example HDD B
3: 12000
4: 53
3 words, the last one is finished

Exercise

Cut a path at / and print its last part.