sodalive-ios/SodaLive/Sources/Extensions/StringExtension.swift

48 lines
1.4 KiB
Swift

//
// StringExtension.swift
// SodaLive
//
// Created by klaus on 2022/06/03.
//
import Foundation
extension Optional where Wrapped == String {
func isNullOrBlank() -> Bool {
return self == nil || self!.trimmingCharacters(in: .whitespaces).isEmpty
}
}
extension String {
func convertDateFormat(from: String, to: String, locale: Locale? = nil) -> String {
let fromFormatter = DateFormatter()
fromFormatter.dateFormat = from
fromFormatter.timeZone = TimeZone(identifier: TimeZone.current.identifier)
if let locale = locale {
fromFormatter.locale = locale
}
if let date = fromFormatter.date(from: self) {
return date.convertDateFormat(dateFormat: to)
} else {
return self
}
}
func substring(from: Int, to: Int) -> String {
guard from < count, to >= 0, to - from >= 0 else {
return ""
}
// Index
let startIndex = index(self.startIndex, offsetBy: from)
let endIndex = index(self.startIndex, offsetBy: to + 1) // '+1' : endIndex
//
return String(self[startIndex ..< endIndex])
// - https://ios-development.tistory.com/379
}
}