我有一个字符串数组,但所有的stings都是用大写字母写的。有没有办法让我将数组中的所有字符串设置为小写(和大写)
array = ["BOY","GIRL","MAN"]
// func to convert it to array = ["Boy","Girl","Man"]
有没有办法做到这一点,而不用小写字母重写数组的内容。我用大写字母组成了很长的字符串数组。
答案 0 :(得分:18)
你可以使用这样的地图功能:
let array = ["BOY","GIRL","MAN"]
let capitalizedArray = array.map { $0.capitalizedString}
答案 1 :(得分:2)
SWIFT 4:
属性capitalizedString已更改为大写。
let array = ["BOY","GIRL","MAN"]
let capitalizedArray = array.map {$0.capitalized}