Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active July 25, 2019 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KentarouKanno/3e4f1404358c1c2f66e32755ab571a54 to your computer and use it in GitHub Desktop.
Save KentarouKanno/3e4f1404358c1c2f66e32755ab571a54 to your computer and use it in GitHub Desktop.
Range Description

Range

// 1から5まで
let oneTofive = 1...5

/* Int */
let low = oneTofive.lowerBound
let upper = oneTofive.upperBound

/* ClosedRange<Int>.Index */
let startIndex = oneTofive.startIndex
let endIndex = oneTofive.endIndex
  • Range<Int>
// 1から5未満
let oneTolessthanfive = 1..<5
  • PartialRangeFrom<Int>
// 1以上
let morethanOne = 1...
  • PartialRangeThrough<Int>
// 5以下
let lessthanFive = ...5
  • PartialRangeUpTo<Int>
// 5未満
let lessthanFive = ..<5

substring

let text : String = "abcdefg"
  • String.SubSequence
// 先頭〜5文字目まで
let text1 = text[text.startIndex...text.index(text.startIndex, offsetBy: 4)]
let text2 = text[text.startIndex..<text.index(text.startIndex, offsetBy: 5)]
let text3 = text[..<text.index(text.startIndex, offsetBy: 5)]

// 2文字目から末尾まで
let text1 = text[text.index(text.startIndex, offsetBy: 1)...]
let text2 = text[text.index(text.startIndex, offsetBy: 1)...text.index(before: text.endIndex)]
let text3 = text[text.index(text.startIndex, offsetBy: 1)..<text.endIndex]

// 2〜4文字目
let text1 = text[text.index(text.startIndex, offsetBy: 1)...text.index(text.startIndex, offsetBy: 3)]
let text2 = text[text.index(text.startIndex, offsetBy: 1)..<text.index(text.startIndex, offsetBy: 4)]

let from = text.index(text.startIndex, offsetBy: 1)
let to = text.index(from, offsetBy: 2)
let text3 = text[from...to]
// 先頭から5文字
let text1 = text.prefix(5)

// 末尾から3文字
let text1 = text.suffix(3)

// 末尾から2文字削る
let text1 = text.prefix(text.count - 2)

extension

extension String {
    
    func substring(_ r: CountableRange<Int>) -> String {
        
        let length = self.count
        let fromIndex = (r.startIndex > 0) ? self.index(self.startIndex, offsetBy: r.startIndex) : self.startIndex
        let toIndex = (length > r.endIndex) ? self.index(self.startIndex, offsetBy: r.endIndex) : self.endIndex
        
        if fromIndex >= self.startIndex && toIndex <= self.endIndex {
            return String(self[fromIndex..<toIndex])
        }
        
        return String(self)
    }
    
    func substring(_ r: CountableClosedRange<Int>) -> String {
        
        let from = r.lowerBound
        let to = r.upperBound
        
        return self.substring(from..<(to+1))
    }
    
    func substring(_ r: CountablePartialRangeFrom<Int>) -> String {
        
        let from = r.lowerBound
        let to = self.count
        
        return self.substring(from..<to)
    }
    
    func substring(_ r: PartialRangeThrough<Int>) -> String {
        
        let from = 0
        let to = r.upperBound
        
        return self.substring(from..<to)
    }
    
    func substring(_ r: PartialRangeUpTo<Int>) -> String {
        
        let from = 0
        let to = r.upperBound
        
        return self.substring(from..<to)
    }
}

Example

let data = "ABCD//EFGH"
let result = data.range(of: "//")

if let theRange = result{
    /* EFGH */
    print(data[theRange.upperBound...])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment