Help us understand the problem. What is going on with this article?

文字列を任意の文字列で分割する方法

More than 3 years have passed since last update.

例えば、

2015.12.11 14:34:502015.12.1114:34:50 のように、間の半角空白で分割する。

環境

  • Xcode 7.1.1
  • Swift 2.1

方法

componentsSeparatedByString(separator: String) -> [String]を使用する。

- 分割に指定する文字列は削除される
- 分割後は配列が返される
- 最初または最後に分割指定文字列が含まれていた場合、該当インデックスの値は空""になる

let title = "2015.12.11 14:34:50"
let titleArray = title.componentsSeparatedByString(" ")
print(titleArray[0])  // 2015.12.11
print(titleArray[1])  // 14:34:50
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away