0
1

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.

[Swift3] 文字列操作

Last updated at Posted at 2016-11-05

Swift3での文字列操作をまとめる。

部分文字列の取得

先頭文字取得

let str = "1234567890"

// 先頭文字から一文字分を進める
let index = str.index(after: str.startIndex)
let partStr = str.substring(to: index)

print(partStr)
/* 1 */

先頭文字以降の文字列取得

let str = "1234567890"

// 先頭文字から一文字分を進める
let index = str.index(after: str.startIndex)
let partStr = str.substring(from: index)

print(partStr)
/* 234567890 */

末尾文字取得

let str = "1234567890"

// 末尾文字から一文字分戻る
let index = str.index(before: str.endIndex)
let partStr = str.substring(from: index)

print(partStr)
/* 0 */

末尾以前までの文字列を取得

let str = "1234567890"

// 末尾文字から一文字分戻る
let index = str.index(before: str.endIndex)
let partStr = str.substring(to: index)

print(partStr)
/* 123456789 */

間の文字列を取得

let str = "1234567890"

let startIndex = str.index(str.startIndex, offsetBy: 3)
let endIndex = str.index(str.endIndex, offsetBy: -2)
let partStr = str.substring(with: startIndex..<endIndex)

print(partStr)
/* 45678 */
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?