1
3

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.

[Swift] お気楽Substring

Posted at

以下で取得してるものはすべてStringではなくSubstringです

##部分文字列

部分文字列が欲しい!ググろう!

// 文字列 "Hoge" を取り出す
let string = "pre_Hoge_suf"
let substring = string[string.index(string.startIndex, offsetBy: 4)..<string.index(string.endIndex, offsetBy: -4)]

なんだこれ? ややこしすぎるやろ!! 頭おかしい

###これでいい

// 文字列 "Hoge" を取り出す
let string = "pre_Hoge_suf"
let substring = string.dropFirst(4).dropLast(4)

##いろいろな部分文字列

###先頭 n文字

let substring =  string.prefix(n)

###末尾 n文字

let substring = string.suffix(n)

###先頭n文字削除(n+1文字目から最後まで)

let substring = string.dropFirst(n)

###末尾n文字削除(先頭からn-1文字目まで)

let substring = string.dropLast(n)

###n文字目からm文字目まで (n < m)

let substring = string.prefix(m).dropFirst(n - 1)

##でも遅いんでしょ?

遅くないです!

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?