
基于Swift4,最后一題字符串處理的題目可以通過字符串索引的方式來做:
func reversedByWord(sentence: String) -> String {
var lastWordIndex = sentence.startIndex
var copySentence = sentence
var ans = ""
copySentence.append(" ")
for i in copySentence.indices {
if copySentence[i] == " " {
let subString = String(copySentence[lastWordIndex..<i].reversed())
lastWordIndex = copySentence.index(after: i)
ans += subString + " "
}
}
ans.removeLast()
return ans
}
reversedByWord(sentence: "The quick brown fox jumps over the lazy dog")
func reversedSantence(sentence: String) -> String {
let ans = String(reversedByWord(sentence: sentence).reversed())
return ans
}
reversedSantence(sentence: "the sky is blue")
上次講解了基本的語法和一些Swift的小技巧捏萍。這期我們來看幾個(gè)最基本的數(shù)據(jù)結(jié)構(gòu):數(shù)組,字符串空闲,集合和字典令杈。 數(shù)組 數(shù)組是最基本的數(shù)據(jù)結(jié)構(gòu)。Swift中改變了以前Objecti...