Last active
September 12, 2015 18:46
-
-
Save masters3d/2ae2c03c2d0074dca248 to your computer and use it in GitHub Desktop.
Split string at camel case assurances, return array of substrings
This file contains hidden or 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
func splitAtCamelCase(inString: String, var result:[String] = []) -> [String] { | |
var previousLetter:String = "" | |
var lastSplitIndex = 0 | |
if !inString.isEmpty { previousLetter = inString.characters.first.map{String($0)} ?? "" } | |
for (index, currentLetter) in inString.characters.map({String($0)}).enumerate(){ | |
if previousLetter.isLowercase && currentLetter.isUppercase{ | |
let rangeWord = Range<Int>(start: lastSplitIndex, end: index) | |
result.append(inString.substringWithRangeInt(rangeWord)) | |
lastSplitIndex = index | |
} | |
previousLetter = currentLetter | |
} | |
result.append(inString.substringFromIndexInt(lastSplitIndex)) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment