Last active
August 19, 2016 07:02
-
-
Save gitbricho/6827894337c6462365b2 to your computer and use it in GitHub Desktop.
swift 2.1 基本:文字列と文字
This file contains hidden or 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
//## 文字列 ##### | |
//## 長さ | |
"abcde".characters.count //5 | |
"アイウエオ".characters.count //5 |
This file contains hidden or 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 l結合文字列 = "aa" + "bb" + "cc" //"aabbcc" | |
//## 配列に分割 | |
//(--- Swift3 ---) update 16/06/15 | |
//let l文字列配列1 = "aa bb cc".componentsSeparatedByString(" ") //["aa", "bb", "cc"] | |
let l文字列配列1 = "aa bb cc".components(separatedBy: " ") //["aa", "bb", "cc"] | |
//## 配列を結合して文字列にする | |
//# メソッドを使用 | |
//(--- Swift3 ---) update 16/06/15 | |
//let l結合文字列2 = ["aa", "bb", "cc"].joinWithSeparator(" ") //"aa bb cc" | |
let l結合文字列2 = ["aa", "bb", "cc"].joined(separator: " ") //"aa bb cc" | |
//# クロージャによる方法 | |
let l結合文字列3 = ["aa","bb","cc"].reduce("") { | |
// x が "" の時は xi を、それ以外は x + xi を次の x として返す | |
if $0 == "" { | |
return $1 | |
} else { | |
return $0 + $1 | |
} | |
} | |
print(l結合文字列3) //"aabbcc" | |
This file contains hidden or 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
//## 文字列の置換 | |
//(--- Swift3 ---) update 16/06/15 | |
//let l置換文字列1 = "aa bb cc".stringByReplacingOccurrencesOfString( | |
// "bb", withString: "BB") //"aa BB cc" | |
let l置換文字列1 = "aa bb cc".replacingOccurrences(of: "bb", with: "BB") //"aa BB cc" | |
//# 正規表現 | |
//(--- Swift3 ---) update 16/06/15 | |
//let l置換文字列2 = "abcde[2000]fg".stringByReplacingOccurrencesOfString( | |
// "\\[\\d{4}\\]", withString: "--", | |
// options:NSStringCompareOptions.RegularExpressionSearch, | |
// range: nil) //"abcde--fg" | |
let l置換文字列2 = "abcde[2000]fg".replacingOccurrences( | |
of: "\\[\\d{4}\\]", with: "--", | |
options:NSString.CompareOptions.regularExpressionSearch, | |
range: nil) //"abcde--fg" | |
//# 範囲指定 | |
var vStr = "abcde" | |
//(--- Swift3 ---) update 16/06/15 | |
//vStr.replaceRange(vStr.startIndex.advancedBy(1)..<vStr.startIndex.advancedBy(4), with: "xyz") //axyze | |
let idx1 = vStr.index(vStr.startIndex, offsetBy: 1) | |
let idx4 = vStr.index(vStr.startIndex, offsetBy: 4) | |
vStr.replaceSubrange(idx1..<idx4, with: "xyz") //axyze |
This file contains hidden or 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
//## 文字列の削除 | |
//(--- Swift3 ---) update 16/06/15 | |
//vStr.removeRange(vStr.startIndex.advancedBy(1)..<vStr.startIndex.advancedBy(3)) //aze | |
let idx3 = vStr.index(vStr.startIndex, offsetBy: 3) | |
vStr.removeSubrange(idx1..<idx3) //aze |
This file contains hidden or 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
//## 部分文字列 | |
//(--- Swift3 ---) update 16/06/15 | |
//let l部分3から最後 = "abcdefg".substringFromIndex(3) //"defg" | |
//(--- Swift3 beta6 ---) update 16/08/19 | |
//let l部分3から最後 = "abcdefg".substring(from: 3) //"defg" | |
let index3 = "abcdefg".index("abcdefg".startIndex, offsetBy: 3) | |
let l部分3から最後 = "abcdefg".substring(from: index3) //"defg" | |
//(--- Swift3 ---) update 16/06/15 | |
//let l部分先頭から3 = "abcdefg".substringToInde(3) //"abc" | |
//(--- Swift3 beta6 ---) update 16/08/19 | |
//let l部分先頭から3 = "abcdefg".substring(to: 3) //"abc" | |
let l部分先頭から3 = "abcdefg".substring(to: index3) //"abc" | |
//(--- Swift3 ---) update 16/06/15 | |
//let l部分3から5 = "abcdefg".substringWithRange(NSRange(location: 3, length: 2)) //"de" | |
//(--- Swift3 beta6 ---) update 16/08/19 | |
//let l部分3から5 = "abcdefg".substring(with: NSRange(location: 3, length: 2)) //"de" | |
let index5 = "abcdefg".index("abcdefg".startIndex, offsetBy: 5) | |
let l部分3から5 = "abcdefg".substring(with: index3..<index5) //"de" |
This file contains hidden or 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
//## トリミング | |
//(--- Swift3 ---) update 16/06/15 | |
//let l先頭と末尾の空白を除去 = " abcdefg ".stringByTrimmingCharactersInSet( | |
// NSCharacterSet.whitespaceCharacterSet()) //"abcdefg" | |
let l先頭と末尾の空白を除去 = " abcdefg ".trimmingCharacters( | |
in: NSCharacterSet.whitespaces()) //"abcdefg" |
This file contains hidden or 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 lStr = "abcdefg" | |
let l先頭 = lStr[lStr.startIndex] //a | |
//(--- Swift3 ---) update 16/06/15 | |
let l先頭の次 = lStr[lStr.index(lStr.startIndex, offsetBy: 1)] //b | |
//(--- Swift3 ---) update 16/06/15 | |
//let l3文字目 = lStr[lStr.startIndex.advancedBy(2)] //c | |
let l3文字目 = lStr[lStr.index(vStr.startIndex, offsetBy: 2)] //c | |
let l先頭から最後 = lStr[lStr.startIndex ..< lStr.endIndex] //abcdefg |
This file contains hidden or 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
//## URL の検査 | |
let l検査文字列 = "この文から URL http://www.google.com を取り出す" | |
//(--- Swift3 ---) update 16/06/15 | |
//let l検査 = try! NSDataDetector(types: NSTextCheckingType.Link.rawValue) | |
//(--- Swift3 beta6 ---) update 16/08/19 | |
//let l検査 = try! NSDataDetector(types: TextCheckingResult.CheckingType.link.rawValue) | |
let l検査 = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) | |
//(--- Swift3 ---) update 16/06/15 | |
//let matches = l検査.matchesInString(l検査文字列, options: [], range: NSMakeRange(0, l検査文字列.characters.count)) | |
let matches = l検査.matches(in: l検査文字列, options: [], range: NSMakeRange(0, l検査文字列.characters.count)) | |
for match in matches { | |
//(--- Swift3 ---) update 16/06/15 | |
//let url = (l検査文字列 as NSString).substringWithRange(match.range) | |
let url = (l検査文字列 as NSString).substring(with: match.range) | |
print(url) | |
} | |
//結果: "http://www.google.com" |
This file contains hidden or 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
//## 書式設定 | |
//# 日付 | |
//(--- Swift3 ---) update 16/06/15 | |
//let df:NSDateFormatter = NSDateFormatter() | |
let df:DateFormatter = DateFormatter() | |
df.dateFormat = "yyyy年MM月dd日" | |
//(--- Swift3 ---) update 16/06/15 | |
//print("日付:\(df.stringFromDate(NSDate()))") | |
print("日付:\(df.string(from: NSDate() as Date))") | |
//結果:"日付:2016年03月01日" (実行した日付による) | |
//# 数値 | |
let num1 = String(format: "%1.2f", 55.3) | |
let num2 = String(format: "%1.2f", 55.345) | |
let num3 = String(format: "%4d", 55) | |
let num4 = String(format: "%04d", 55) | |
print("f1:\(num1) f2:\(num2) i1:\(num3) i2:\(num4)") | |
//結果:"f1:55.30 f2:55.34 i1: 55 i2:0055" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment