Swift에서 substring
함수를 구현하는 방법을 알아보자.
extension String { func substring(from: Int, to: Int) -> String { guard from < count, to >= 0, to - from >= 0 else { return "" } let startIndex = index(self.startIndex, offsetBy: from) let endIndex = index(self.startIndex, offsetBy: to + 1) // '+1'이 있는 이유: endIndex는 문자열의 마지막 그 다음을 가리키기 때문 return String(self[startIndex ..< endIndex]) } }
let subStr = str.substring(from: i, to: i+len-1)
dropfirst()
& prefix()
let subStr = str.dropFirst(i).prefix(p.count)
→ 1번보다는 2번이 실행속도가 더 빠른 것 같다!