mirror of
https://github.com/selfxyz/self.git
synced 2026-02-09 05:46:01 -05:00
41 lines
1.2 KiB
Swift
41 lines
1.2 KiB
Swift
//
|
|
// StringExt.swift
|
|
// NFCPassportReaderApp
|
|
//
|
|
// Created by Andy Qua on 30/06/2019.
|
|
// Copyright © 2019 Andy Qua. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// Some Utility methods for string - access characters by index
|
|
extension String {
|
|
subscript(_ i: Int) -> String {
|
|
let idx1 = index(startIndex, offsetBy: i)
|
|
let idx2 = index(idx1, offsetBy: 1)
|
|
return String(self[idx1..<idx2])
|
|
}
|
|
|
|
subscript (bounds: CountableRange<Int>) -> String {
|
|
let start = index(startIndex, offsetBy: bounds.lowerBound)
|
|
let end = index(startIndex, offsetBy: bounds.upperBound)
|
|
return String(self[start..<end])
|
|
}
|
|
|
|
subscript (bounds: CountableClosedRange<Int>) -> String {
|
|
let start = index(startIndex, offsetBy: bounds.lowerBound)
|
|
let end = index(startIndex, offsetBy: bounds.upperBound)
|
|
return String(self[start...end])
|
|
}
|
|
|
|
subscript (bounds: CountablePartialRangeFrom<Int>) -> String {
|
|
let start = index(startIndex, offsetBy: bounds.lowerBound)
|
|
return String(self[start...])
|
|
}
|
|
|
|
func strip() -> String {
|
|
let trimmed = self.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
return trimmed
|
|
}
|
|
}
|