swift 首字母大写处理,区分标题和句子
样例1:
let temStr1 = "this is first sentence. this is second sentence."
print(temStr1.capitalized)
This Is First Sentence. This Is Second Sentence.
样例2:
let temStr2 = "this is first sentence. this is second sentence."
print(temStr2.capitalizingFirstLetter())
This is first sentence. This is second sentence.
结论:如果只是标题需要做成样例1,是句子需要做成样例2。
说明:
1. capitalized是swift 系统属性;
2.capitalizingFirstLetter是字体String的拓展方法,如下:
extension String { func capitalizingFirstLetter() -> String { return prefix(1).capitalized + dropFirst() } mutating func capitalizeFirstLetter() { self = self.capitalizingFirstLetter() } }