Created
May 6, 2024 06:46
-
-
Save kelvinfok/29f1a90976fb85a04ddaaeff9574687e to your computer and use it in GitHub Desktop.
ios-udemy-home String extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
var camelCaseToEnglish: String { | |
var result = "" | |
var wordStartIndex = self.startIndex | |
for (index, char) in self.enumerated() { | |
if index > 0 && char.isUppercase { | |
let previousWord = self[wordStartIndex..<self.index(self.startIndex, offsetBy: index)] | |
if !previousWord.isEmpty { | |
result += "\(previousWord) " | |
} | |
wordStartIndex = self.index(self.startIndex, offsetBy: index) | |
} | |
} | |
let lastWord = self[wordStartIndex..<self.endIndex] | |
if !lastWord.isEmpty { | |
result += "\(lastWord)" | |
} | |
return result | |
} | |
var useShortAndFormat: String { | |
replacingOccurrences(of: "And", with: " & ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment