Skip to content

Instantly share code, notes, and snippets.

@NickHung1982
Created February 1, 2019 15:14
Show Gist options
  • Save NickHung1982/7e55ed93e3d1d1a9fcd0807bff9ae6d7 to your computer and use it in GitHub Desktop.
Save NickHung1982/7e55ed93e3d1d1a9fcd0807bff9ae6d7 to your computer and use it in GitHub Desktop.
ConvertStringToArray
func splitCapitalString(_ textString:String) -> [String] {
var newStringArray = [String]()
for char in textString {
if String(char) == String(char).uppercased() {
newStringArray.append(" ")
}
newStringArray.append(String(char))
}
//joined 把加工後的array 變成字串 "HeMoYo" -> " He Mo Yo"
//trimmingCharacters 移除左右空白
//compoents 把空白拆開為Array, 也可以用 Split(separator:), 但是會回傳 SubSequence type
return newStringArray.joined().trimmingCharacters(in: .whitespacesAndNewLines).compoents(separatedBy: " ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment