Skip to content

Instantly share code, notes, and snippets.

@MTattin
Created October 8, 2016 01:40
Show Gist options
  • Save MTattin/357b47ec48a92ce52912b30c5859803a to your computer and use it in GitHub Desktop.
Save MTattin/357b47ec48a92ce52912b30c5859803a to your computer and use it in GitHub Desktop.
swift3の文字列操作(String.substring)メモ ref: http://qiita.com/MTattin/items/bf32562cd7d03c49026f
let Str: String = "123456789"
///
/// 先頭1文字 - 1
///
print("\(Str.substring(to: Str.index(after: Str.startIndex)))")
///
/// 先頭1文字以外 - 23456789
///
print("\(Str.substring(from: Str.index(after: Str.startIndex)))")
///
/// 末尾1文字 - 9
///
print("\(Str.substring(from: Str.index(before: Str.endIndex)))")
///
/// 末尾1文字以外 - 12345678
///
print("\(Str.substring(to: Str.index(before: Str.endIndex)))")
///
/// 先頭から?文字 - 12345
///
print("\(Str.substring(to: Str.index(Str.startIndex, offsetBy: 5)))")
///
/// 末尾から?文字 - 56789
///
print("\(Str.substring(from: Str.index(Str.endIndex, offsetBy: -5)))")
///
/// 先頭?文字以外 - 6789
///
print("\(Str.substring(from: Str.index(Str.startIndex, offsetBy: 5)))")
///
/// 末尾?文字以外 - 1234
///
print("\(Str.substring(to: Str.index(Str.endIndex, offsetBy: -5)))")
///
/// ?から?文字 - 456
///
print("\(Str.substring(with: Str.index(Str.startIndex, offsetBy: 3)..<Str.index(Str.endIndex, offsetBy: -3)))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment