Files
self/app/ios/Examples/Example_SPM/NFCPassportReaderApp/Extensions/StringExt.swift
2023-10-14 14:46:20 +02:00

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
}
}