Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize the first character of sentence using Swift

I have a String description that holds my sentence and want to capitalize only the first letter. I tried different things but most of them give me exceptions and errors. I'm using Xcode 6.

Here is what I tried so far:

let cap = [description.substringToIndex(advance(0,1))] as String
    description = cap.uppercaseString + description.substringFromIndex(1)

It gives me:

Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible'

I tried:

 func capitalizedStringWithLocale(locale:0) -> String

But I haven't figured out how to make it work.

like image 429
Romaldowoho Avatar asked Nov 22 '14 21:11

Romaldowoho


People also ask

How do you capitalize characters in Swift?

Swift String uppercased() The uppercased() method converts all lowercase characters in a string into uppercase characters.

How do I get the first character of a string in Swift?

Use the startIndex property to access the position of the first Character of a String . The endIndex property is the position after the last character in a String . As a result, the endIndex property isn't a valid argument to a string's subscript. If a String is empty, startIndex and endIndex are equal.


3 Answers

In Swift 2, you can do

String(text.characters.first!).capitalizedString + String(text.characters.dropFirst())
like image 138
Andreas Avatar answered Oct 27 '22 11:10

Andreas


Another possibility in Swift 3:

extension String {
    func capitalizeFirst() -> String {
        let firstIndex = self.index(startIndex, offsetBy: 1)
        return self.substring(to: firstIndex).capitalized + self.substring(from: firstIndex).lowercased()
    }
}

For Swift 4:

Warnings from above Swift 3 code:

 'substring(to:)' is deprecated: Please use String slicing subscript
 with a 'partial range upto' operator.   
'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.  

Swift 4 solution:

extension String {
    var capitalizedFirst: String {
        guard !isEmpty else {
            return self
        }

        let capitalizedFirstLetter  = charAt(i: 0).uppercased()
        let secondIndex             = index(after: startIndex)
        let remainingString         = self[secondIndex..<endIndex]

        let capitalizedString       = "\(capitalizedFirstLetter)\(remainingString)"
        return capitalizedString
    }
}
like image 5
Ajith R Nayak Avatar answered Oct 27 '22 09:10

Ajith R Nayak


Swift 5.0

Answer 1:

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).capitalized + dropFirst()
     }

    mutating func capitalizeFirstLetter() {
        self = self.capitalizingFirstLetter()
    }
 }

Answer 2:

 extension String {
       func capitalizeFirstLetter() -> String {
            return self.prefix(1).capitalized + dropFirst()
       }
  }

Answer 3:

 extension String {
       var capitalizeFirstLetter:String {
            return self.prefix(1).capitalized + dropFirst()
       }
  }
like image 4
Sazzadhusen Iproliya Avatar answered Oct 27 '22 09:10

Sazzadhusen Iproliya