Capital letter char sentences (Swift)

I have a line description that contains my sentence. I only want to make amends for the first letter. I tried different things, but most of them give me exceptions and errors. Im using Xcode 6.

Here is what I have tried so far

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

This gives me: Type 'String.Index' does not comply with IntegerLiteralConvertible protocol

I tried

  func capitalizedStringWithLocale(locale:0) -> String 

But I just can't figure out how to make it work. Any ideas?

+6
source share
9 answers
 import Foundation // A lowercase string let description = "the quick brown fox jumps over the lazy dog." // The start index is the first letter let first = description.startIndex // The rest of the string goes from the position after the first letter // to the end. let rest = advance(first,1)..<description.endIndex // Glue these two ranges together, with the first uppercased, and you'll // get the result you want. Note that I'm using description[first...first] // to get the first letter because I want a String, not a Character, which // is what you'd get with description[first]. let capitalised = description[first...first].uppercaseString + description[rest] // Result: "The quick brown fox jumps over the lazy dog." 

You can make sure that before you start, make sure that the sentence has at least one character, because otherwise you will get a run-time error trying to push the index out of line.

+2
source

In Swift 2, you can do String(text.characters.first!).capitalizedString + String(text.characters.dropFirst())

+8
source

Another feature in Swift 3 is

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

Edit for Swift 4
Warnings from above Swift 3 code -

'substring (to :)' is deprecated: use a line scan index with the 'partial range to' operator.
'substring (from :)' is deprecated: use a line scan index with the '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 } } 
+4
source

Here's how to do it in Swift 4; just in case this helps someone:

 extension String { func captalizeFirstCharacter() -> String { var result = self let substr1 = String(self[startIndex]).uppercased() result.replaceSubrange(...startIndex, with: substr1) return result } } 

It will not mutate the original String .

+1
source
 extension String { var capitalizedFirstLetter:String { let string = self return string.replacingCharacters(in: startIndex...startIndex, with: String(self[startIndex]).capitalized) } } 

Answer

 let newSentence = sentence.capitalizedFirstLetter 
+1
source

For one or each word in a string, you can use the String.capitalized property.

 print("foo".capitalized) //prints: Foo print("foo foo foo".capitalized) //prints: Foo Foo Foo 
0
source

The simplest soul for Swift 4.0

Add as an extension of a computed property

 extension String { var firstCapitalized: String { var components = self.components(separatedBy: " ") guard let first = components.first else { return self } components[0] = first.capitalized return components.joined(separator: " ") } } 

Using

 "hello world".firstCapitalized 
0
source

Swift Version 4.2:

 extension String { var firstCharCapitalized: String { switch count { case 0: return self case 1: return uppercased() default: return self[startIndex].uppercased() + self[index(after: startIndex)...] } } } 
0
source

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() } } 
0
source

Source: https://habr.com/ru/post/1207487/


All Articles