19
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Stringの文字列を1文字ずつ区切る

Last updated at Posted at 2015-09-28

よく、","(カンマ)や特定の文字列で分割とかは見かけるのですが、今回はStringの文字列を1文字ずつ区切る方法を。

let text = "foobar"
let characters = text.characters.map { String($0) } // String -> [String]
print(characters)
// ["f", "o", "o", "b", "a", "r"]

text.charactersのところで、文字列が1文字ずつに分割されますが、この時のcharactersの型が、[CharacterView]となっていて、このままではStringとして使うときに不便なので、mapをかませて、CharacterView→Stringと戻してあげます。

19
15
2

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
19
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?