2014年8月15日金曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 7(Using Methods)、7.6(Exercises) 8-a, b, c.をSwiftで考えてみる。

7.6(Exercises) 8-a, b, c.

コード(Xcode)

string.swift

//
//  string.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

extension String {
    var swapcaseString:String {
    var result: String = ""
        for ch in self {
            let s = String(ch)
            result += s.uppercaseString == s ? s.lowercaseString : s.uppercaseString
        }
        return result
    }
    var isUpper:Bool { return self.uppercaseString == self }
    var isLower:Bool { return self.lowercaseString == self }
    func count(sub:String) -> Int {
        var result:Int = 0
        var s = self
        let index:String.Index = "a".endIndex
        while s != "" {
            if s.hasPrefix(sub) {
                result += 1
                s = s.substringFromIndex(sub.endIndex)
            } else {
                s = s.substringFromIndex(index)
            }
            
        }
        return result
    }
    func find(sub:String, start:Int = 0, end:Int? = nil) -> Int {
        var s = self as NSString
        var s_temp = s.substringFromIndex(start)
        let end_temp:Int = end ? end! : s.length
        for i in start..<end_temp {
            if (s_temp.hasPrefix(sub)) {
                return i
            }
            s_temp = s.substringFromIndex(i + 1) as NSString
        }
        return -1
    }
    func replace(old_str:String, new_str:String) -> String {
        return self.stringByReplacingOccurrencesOfString(old_str, withString: new_str)
    }
    func strip() -> String {
        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }
    func slice(start:Int=0, end:Int?=nil) -> String {
        let s = self as NSString
        var new_end:Int
        if (end) {
            new_end = end!
        } else {
            new_end = s.length
        }
        return  s.substringWithRange(NSRange(location: start, length:new_end - start))
    }
}

main.swift

//
//  main.swift
//  sample8
//
//  Created by kamimura on 8/15/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

let fruit = "pineapple"

println("a.")
println(fruit.count("p"))  // 3
println(fruit.find("p", start: 3)) // 5
println(fruit.find("p", start: fruit.count("p"))) // 5

println("b.")
println(fruit.uppercaseString)       // PINEAPPLE
println("PINEAPPLE".swapcaseString)  // pineapple
println(fruit.count("pineapple"))    // 1
println(fruit.count(fruit.uppercaseString.swapcaseString)) // 1

println("c.")
println(fruit.swapcaseString) // PINEAPPLE
println(fruit.lowercaseString) // pineapple
println(fruit.replace("PINEAPPLE", new_str: "pineapple")) // pineapple
println(fruit.replace(fruit.uppercaseString, new_str: fruit.lowercaseString)) // pineapple

入出力結果(Console Output)

a.
3
5
5
b.
PINEAPPLE
pineapple
1
1
c.
PINEAPPLE
pineapple
pineapple
pineapple
Program ended with exit code: 0

0 コメント:

コメントを投稿