44 lines
1.2 KiB
Swift
44 lines
1.2 KiB
Swift
//
|
|
// StringExtension.swift
|
|
// yozm
|
|
//
|
|
// 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) -> String {
|
|
let fromFormatter = DateFormatter()
|
|
fromFormatter.dateFormat = from
|
|
fromFormatter.timeZone = TimeZone(identifier: TimeZone.current.identifier)
|
|
|
|
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
|
|
}
|
|
}
|