Algorithm for joining e.g. an array of strings

后端 未结 16 1775
陌清茗
陌清茗 2021-01-01 21:40

I have wondered for some time, what a nice, clean solution for joining an array of strings might look like. Example: I have [\"Alpha\", \"Beta\", \"Gamma\"] and want to join

16条回答
  •  孤街浪徒
    2021-01-01 22:03

    join() function in Ruby:

    def join(seq, sep) 
      seq.inject { |total, item| total << sep << item } or "" 
    end
    
    join(["a", "b", "c"], ", ")
    # => "a, b, c"
    

提交回复
热议问题