Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to change a certain character from a string

Hi, I am working on a program that can make the first letter of every word to be capitalised. So for example, if I pass in the string "new york" to this function, it will return "New York". I am currently working on how to change that character, I already made an extension to get the character at a certain index, but I cannot modify it. So can someone help me out with that. Thanks

extension String {

    subscript (i: Int) -> Character {
        return self[advance(self.startIndex, i)]
    }

    subscript (i: Int) -> String {
        return String(self[i] as Character)
    }

    subscript (r: Range<Int>) -> String {
        return substringWithRange(Range(start: advance(startIndex, r.startIndex), end: advance(startIndex, r.endIndex)))
    }

}

1 Answer

func capitalize(stringToCap:String) ->String {

   return stringToCap.capitalizedString
}

var bigApple = "new york"

capitalize(bigApple)     //"New york"

Oh thanks, I just made it too complicated.