Give a string and return an array with it capital letters.
For example:
Input: "ShowMeTheMoney"
Output: ["Show","Me","The","Money"]
---------------------------------------
解法1:
去巡覽每個字母,如果那個字母是大寫(capital letter),就在後面加一個空格(也可以用其他符號代替,但是用空格最好,晚一點說明)。之後整合成字串後再用Split分開成Array
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 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: " ") | |
} |
留言
張貼留言