facebook twitter hatena line email

「Ios/swift/数字と文字」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(数字を文字へ)
(画像urlを抽出)
 
(同じ利用者による、間の12版が非表示)
行18: 行18:
 
  "hello" + num.description
 
  "hello" + num.description
 
  "hello" + String(num)
 
  "hello" + String(num)
 +
 +
==文字を数字へ==
 +
var id = "1234"
 +
print(Int(id)) // 1234
 +
 +
参考:http://qiita.com/sasagin/items/28c3af9a25151f8ab4a6
 +
 +
==指定した文字が含まれているか==
 +
var url : String = "ttp://example.com"
 +
if url.containsString("http://") || url.containsString("https://") {
 +
    print("httpが含まれる")
 +
}
 +
==文字置換==
 +
let str = "hogepiyo"
 +
str = str.stringByReplacingOccurrencesOfString("ge", withString: "ho") // hohopiyo
 +
 +
==正規表現置換==
 +
var html: String = "hote\nhote"
 +
html = html.stringByReplacingOccurrencesOfString("\n", withString:"<br>", options:NSStringCompareOptions.RegularExpressionSearch, range: nil)
 +
 +
==StringからOptionalを消す==
 +
print(test) // Optional("aaa")
 +
let test2 = test as! String // "aaa"
 +
 +
==正規表現によるurl抽出==
 +
import UIKit
 +
let pattern = "(https?://[-_.!‾*'()a-zA-Z0-9;/?:@&=+$,%#]+)"
 +
let str: String = "<img src=\"http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png\" ><a href=\"http://yahoo.co.jp?page=1#jpg\" >"
 +
let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
 +
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
 +
var results: [String] = []
 +
matches.forEach { (match) -> () in
 +
    results.append( (str as NSString).substringWithRange(match.rangeAtIndex(1)) )
 +
}
 +
print(results)
 +
["http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png", "http://yahoo.co.jp?page=1#jpg"]
 +
 +
==画像urlを抽出==
 +
        let pattern = "(https?://[-_.!‾*'()a-zA-Z0-9;/?:@&=+$,%#]+).(jpg|png|gif|jpeg)"
 +
        let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
 +
        let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
 +
        var results: [String] = []
 +
        matches.forEach { (match) -> () in
 +
            if (str as NSString).substringWithRange(match.rangeAtIndex(2)) != "" {
 +
                    results.append( String((str as NSString).substringWithRange(match.rangeAtIndex(1))) + "." + String((str as NSString).substringWithRange(match.rangeAtIndex(2))) )
 +
            }
 +
        }
 +
        return results
 +
 +
==配列内検索==
 +
phpでいうin_array()
 +
let names: [String] = ['aa', 'bb', 'cc']
 +
let str = 'aa'
 +
if names.contains(str) {

2016年9月1日 (木) 03:16時点における最新版

リアルタイム実行環境構築

  1. xcodeを起動後"get started with a playground"を選択
  2. 左にコードを記述すると、右に結果がリアルタイムで表示される

定義

let a = 1 // letで定数定義
var b = a + 2 // varで変数定義
var str = "Hello, playground"

データ型定義

Int, Double, Float, String, Bool, Stringなどがある

var a:Int = 1
var c:UInt = 10 // 正の数のみ
var d:Int32 = 4 // 32bit数

数字を文字へ

let num = 1
"hello" + num.description
"hello" + String(num)

文字を数字へ

var id = "1234"
print(Int(id)) // 1234

参考:http://qiita.com/sasagin/items/28c3af9a25151f8ab4a6

指定した文字が含まれているか

var url : String = "ttp://example.com"
if url.containsString("http://") || url.containsString("https://") {
    print("httpが含まれる")
}

文字置換

let str = "hogepiyo"
str = str.stringByReplacingOccurrencesOfString("ge", withString: "ho") // hohopiyo

正規表現置換

var html: String = "hote\nhote"
html = html.stringByReplacingOccurrencesOfString("\n", withString:"
", options:NSStringCompareOptions.RegularExpressionSearch, range: nil)

StringからOptionalを消す

print(test) // Optional("aaa")
let test2 = test as! String // "aaa"

正規表現によるurl抽出

import UIKit
let pattern = "(https?://[-_.!‾*'()a-zA-Z0-9;/?:@&=+$,%#]+)"
let str: String = "<img src=\"http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png\" ><a href=\"http://yahoo.co.jp?page=1#jpg\" >"
let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
var results: [String] = []
matches.forEach { (match) -> () in
    results.append( (str as NSString).substringWithRange(match.rangeAtIndex(1)) )
}
print(results)
["http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png", "http://yahoo.co.jp?page=1#jpg"]

画像urlを抽出

       let pattern = "(https?://[-_.!‾*'()a-zA-Z0-9;/?:@&=+$,%#]+).(jpg|png|gif|jpeg)"
       let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
       let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
       var results: [String] = []
       matches.forEach { (match) -> () in
           if (str as NSString).substringWithRange(match.rangeAtIndex(2)) != "" {
                   results.append( String((str as NSString).substringWithRange(match.rangeAtIndex(1))) + "." + String((str as NSString).substringWithRange(match.rangeAtIndex(2))) )
           }
       }
       return results

配列内検索

phpでいうin_array()

let names: [String] = ['aa', 'bb', 'cc']
let str = 'aa'
if names.contains(str) {