Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 760
Rhenz Avatar asked Feb 24 '15 07:02

Rhenz


Video Answer


6 Answers

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"]
like image 158
Leo Dabus Avatar answered Oct 20 '22 19:10

Leo Dabus


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]

like image 35
Ashish Kakkad Avatar answered Oct 20 '22 18:10

Ashish Kakkad


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
    })
like image 2
Phssthpok Avatar answered Oct 20 '22 20:10

Phssthpok


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
    }

}
like image 2
Rhenz Avatar answered Oct 20 '22 18:10

Rhenz


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.

like image 1
Antonio Avatar answered Oct 20 '22 19:10

Antonio


Use .uppercaseString to capitalize all characters.

like image 1
Bonga Mbombi Avatar answered Oct 20 '22 20:10

Bonga Mbombi