如何直接将数组中的所有字符串大写?

时间:2015-02-24 07:22:18

标签: ios string swift capitalize

我快速学习。我一直在Playground尝试这个。我不知道为什么字符串没有在这里大写。或者有没有其他方法可以直接在数组中大写字符串?

这是我的代码。

var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]

for index in 0..<dogNames.count {
    var dogName = dogNames[index].capitalizedString
    dogNames.removeAtIndex(index)
    dogNames.append(dogName)
}

当我尝试再次显示变量dogNames时。里面的字符串没有大写。

7 个答案:

答案 0 :(得分:12)

更新: Xcode 8.2.1•Swift 3.0.2

var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]

for (index, element) in dogNames.enumerated() {
    dogNames[index] = element.capitalized
}

print(dogNames)   // "["Sean", "Fido", "Sarah", "Parker", "Walt", "Abby", "Yang"]\n"

这是使用map()的典型案例:

let dogNames1 = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"].map{$0.capitalized}

filter()样本:

let dogNamesStartingWithS = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"].filter{$0.hasPrefix("S")}

dogNamesStartingWithS   // ["Sean", "Sarah"]

你可以将两者结合起来:

let namesStartingWithS = ["sean", "fido", "sarah", "parker", "walt", "abby", "yang"].map{$0.capitalized}.filter{$0.hasPrefix("S")}

namesStartingWithS   // ["Sean", "Sarah"]

您还可以使用方法排序(如果您不想改变原始数组,则进行排序),以便在需要时按字母顺序对结果进行排序:

let sortedNames = ["sean", "fido", "sarah", "parker", "walt", "abby", "yang"].map{$0.capitalized}.sorted()

sortedNames  // ["Abby", "Fido", "Parker", "Sarah", "Sean", "Walt", "Yang"]

答案 1 :(得分:3)

尝试使用以下代码:

var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]

for index in 0..<dogNames.count {
    var dogName = dogNames[index].capitalizedString
    dogNames[index]=dogName
}

输出:

  

[Sean,Fido,Sarah,Parker,Walt,Abby,Yang]

答案 2 :(得分:2)

从数组的中间删除然后追加到最后,最后跳过一些项目。以下是每个步骤中数组的外观:

[Sean, fido, Sarah, Parker, Walt, abby, Yang]
[fido, Sarah, Parker, Walt, abby, Yang, Sean] (index=0; Sean moved to end)
[fido, Parker, Walt, abby, Yang, Sean, Sarah] (index=1; Sarah moved to end)
[fido, Parker, abby, Yang, Sean, Sarah, Walt] (index=2; Walt moved to end)
[fido, Parker, abby, Sean, Sarah, Walt, Yang]
[fido, Parker, abby, Sean, Walt, Yang, Sarah]
[fido, Parker, abby, Sean, Walt, Sarah, Yang]
[fido, Parker, abby, Sean, Walt, Sarah, Yang]

如果你想保持数组的完整性,那么用你从中获取它的相同索引替换它会更有意义:

dogNames[index] = dogName

但是你可以通过使用Array.map独立处理每个项目而不必处理索引来更优雅地做到这一点:

    let dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]
    let capitalDogNames = dogNames.map({ (dogName) -> String in
        return dogName.capitalizedString
    })

答案 3 :(得分:2)

也回答我自己的问题。总结我在答案中找到的所有内容。我想出了这个解决方案。这就是我用较少的过程来解决这个问题。

var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]


for index in 0..<dogNames.count {

    if dogNames[index] != dogNames[index].capitalizedString {
        var dogName = dogNames[index].capitalizedString
        dogNames[index] = dogName
    }

}

答案 4 :(得分:1)

使用.uppercaseString将所有字​​符大写。

答案 5 :(得分:1)

您必须以相反的顺序执行循环:

for index in reverse(0..<dogNames.count)

原因是当你从一个数组中删除一个元素时,被移除的元素之后的所有元素都会向后移动一个位置,因此它们的索引会发生变化 - 而之前的所有元素都没有任何索引更改。通过以相反的顺序导航,您可以确定仍处理的项目的索引没有更改。

答案 6 :(得分:0)

这是在 swift 3.0 中执行此操作的最简单方法:

var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]

dogNames = dogNames.map {$0.capitalized}
print("dogNames: \(dogNames)")