1. 程式人生 > >Ruby--陣列與字串轉換及去重

Ruby--陣列與字串轉換及去重

1 字串轉陣列

  • split(",")
#字串a
a = "12,34,56"
#陣列a
a = a.split(",")
#a = ["12","34","56"]

2 陣列轉成字串

  • join(",")
#陣列
a = ["12","34","56"]
#字串
a = a.join(",")
#a = "12,34,56"

3 陣列去重

  • uinq
#陣列
a = ["12","34","56","56"]
#去重
a = a.uniq
#a = ["12","34","56"]

4 字串去重

#字串
a = "12,34,56,56"
#轉成陣列,去重,轉成字串
a = a.split(",").uniq.join(",")