Last active
August 29, 2015 14:15
-
-
Save yamamotoj/5fecd31eb1386bc67fdd to your computer and use it in GitHub Desktop.
String.Indexを使った文字列処理 ref: http://qiita.com/boohbah/items/795501495e1aeab6231e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABCDEF" | |
let startIndex = str.startIndex // 文字列の最初のIndexを取得 | |
let endIndex = str.endIndex // 文字列中の最後のIndexを取得 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABCDEF" | |
let startIndex = str.startIndex | |
let nextIndex = startIndex.successor() // 次のIndexを取得する | |
let startIndex2 = nextIndex.predecessor() // 一つ前のIndexを取得する | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABCDEF" | |
let startIndex = str.startIndex | |
let idx4 = advance(startIndex, 4) // startIndexから3つ先にすすめる |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABC" | |
let idx0 = str.startIndex | |
let idx1 = idx0.successor() | |
let idx2 = idx1.successor() | |
str[idx0] // => 'A' Character型 | |
str[idx1] // => 'B' Character型 | |
srt[idx2] // => 'C' Character型 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABC" | |
for s in str{ | |
println("s \(s)") | |
} | |
//結果 | |
//s A | |
//s B | |
//s C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index位置(を含む)から文字列の最後までの部分文字列を返す | |
func substringFromIndex(index: String.Index) -> String | |
// 文字列のはじめからindex位置の前までの部分文字列を返す | |
// index位置の文字は含まない | |
func substringToIndex(index: String.Index) -> String | |
// 与えられたindexの範囲の部分文字列を返す | |
func substringWithRange(aRange: Range<String.Index>) -> String | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "ABC" | |
let idx0 = str.startIndex | |
let idx1 = idx0.successor() | |
let idx2 = idx1.successor() | |
str.substringFromIndex(idx1) // => "BC" | |
str.substringToIndex(idx2) // => "AB" | |
// idx1は含むがidx2は含まれない | |
str.substringWithRange(Range(start: idx1, end: idx2)) // => "B" | |
// idx1もidx2も両方含まれる | |
str.substringWithRange(idx1...idx2) // => "BC" | |
// idx1は含むがidx2は含まれない | |
str.substringWithRange(idx1..<idx2) // => "B" | |
// str.substringWithRange(idx1..idx2) => コンパイルエラー(半閉空間の指定の演算子は..<) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment