LoginSignup
42
41

More than 5 years have passed since last update.

Swiftで文字列の分割と連結 (split & join)

Posted at

NSString/NSArrayの時と比べると大分タイプ数が減ってすっきりしました。
joinとsplitはビルドイン関数になりました。

文字列の分割 split

NSArray(旧)
let csvRow = "one,two,three" as NSString
let cells = csvRow.componentsSeparatedByString(",")
// ["one","two","three"]
Array(新)
let csvRow = "one,two,three"
let cells = split(csvRow, { $0 == "," })
// ["one","two","three"]

文字列の連結 join

NSArray(旧)
let cells = ["one", "two", "three"] as NSArray
let csvRow = cells.componentsJoinedByString(",")
// "one,two,three"
Array(新)
let cells = ["one", "two", "three"]
let csvRow = join(",", cells)
// "one,two,three"
42
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
41