How can I capitalize all the strings inside an array directly?

蹲街弑〆低调 提交于 2019-11-29 19:55:08

问题


I'm learning swift. I've been trying this in Playground. I have no idea why the string is not being capitalized here. Or is there any other way to capitalize the string inside the array directly?

Here's my code.

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)
}

When I try to display again the variable dogNames. The strings inside are not being capitalized.


回答1:


update: 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"

This is a typical case for using map():

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

A filter() sample:

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

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

you can combine both:

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

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

You can also use the method sort (or sorted if you don't want to mutate the original array) to sort the result alphabetically if needed:

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

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



回答2:


Try to use following code :

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

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

Output :

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




回答3:


By removing from the middle of the array and then appending to the end, you end up skipping over some items. Here is what the array looks like at each step:

[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]

If you want to keep the array intact, it would make more sense to replace at the same index that you took it from:

dogNames[index] = dogName

But you can do this more elegantly by using Array.map to process each item independently, and not have to deal with indexes at all:

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



回答4:


To answer my own question as well. Summarizing everything I found in the answers here. I came up with this solution. This is what I did to fix this with less process.

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
    }

}



回答5:


You have to perform the loop in reverse order:

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

The reason is that when you remove an element from an array, all elements after the removed one are shifted back by one position, hence having their index changed - whereas all elements before do not have any index change. By navigating in reverse order you are sure that the items still to process haven't had their index changed.




回答6:


Use .uppercaseString to capitalize all characters.




回答7:


Here is the simplest way to do it in swift 3.0:

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

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


来源:https://stackoverflow.com/questions/28690190/how-can-i-capitalize-all-the-strings-inside-an-array-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!