Algorithm for joining e.g. an array of strings -
i have wondered time, nice, clean solution joining array of strings might like. example: have ["alpha", "beta", "gamma"] , want join strings one, separated commas – "alpha, beta, gamma".
now know programming languages offer kind of join method this. wonder how these might implemented. when took introductory courses, tried go alone, never found satisfactory algorithm. seemed rather messy, problem being can not loop through array, concatenating strings, add 1 many commas (either before or after last string). don’t want check conditions in loop. don’t want add first or last string before/after loop (i guess maybe best way?).
can show me elegant solution? or tell me why there can’t more elegant?
the elegant solution found problems (in pseudocode)
separator = "" foreach(item in stringcollection) { concatenatedstring += separator + item separator = "," }
you run loop , after second time around separator set. first time won't added. it's not clean i'd i'd still add comments it's better if statement or adding first or last item outside loop.
Comments
Post a Comment