32
19

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の文字列操作(String.substring)メモ

Posted at

文字列(String)のsubstringの使い方についての覚書。
他にも書き方はあると思いますが。


        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)))")

32
19
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?