Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize first word in every sentence with Swift

Tags:

swift

nsstring

By taking into account of user locale, how can I capitalize the first word of each sentence in a paragraph? What I want to achieve is no matter the case inside the sentence, the first letter of each word will be uppercase and the rest will be lowercase. I can do only one sentence by first converting everything to lower case, then get first letter and make uppercase and finally add them up together. My question is different than How to capitalize each word in a string using Swift iOS since I don't want to capitalize each word. I just want to capitalize the first word of each sentence. capitalizedString turns

"this is first sentence. this is second sentence."

to

"This Is First Sentence. This Is Second Sentence."

What I want is

"This is first sentence. This is second sentence."

My question is also different than Capitalise first letter of every sentence Since @rintaro's code doesn't work on my below example. It keeps capital letters in original text intact. With @rintaro's code;

before

"someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"

after

"SomeSentenceWith UTF text İŞğĞ. AnotherSentenceğüÜğ."

What I want to achieve,

"Somesentencewith utf text işğğ. Anothersentenceğüüğ." 

My code below can only do partial conversion.

var description = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
description = description.lowercaseStringWithLocale(NSLocale.currentLocale())
let first = description.startIndex
let rest = advance(first,1)..<description.endIndex
let capitalised = description[first...first].uppercaseStringWithLocale(NSLocale.currentLocale()) + description[rest]

I will really appreciate if you can please read my question carefully, since this is the third time I am editing the question. I am really sorry if I couldn't ask it clearly since I am not a native speaker. So even though @rintaro answered similar question, his answer doesn't solve my problem. @martin-r suggests a Objective-C answer which again doesn't solve the problem I have. There were another user eric something who also suggested another answer but deleted afterwards. I just can't understand why several people suggest different answer which doesn't answer my question.


2 Answers

Try:

let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"

var result = ""
str.uppercaseString.enumerateSubstringsInRange(indices(str), options: .BySentences) { (sub, _, _, _)  in
    result += sub[sub.startIndex ... sub.startIndex]
    result += sub[sub.startIndex.successor() ..< sub.endIndex].lowercaseString
}

println(result) // -> "Somesentencewith utf text i̇şğğ. Anothersentenceğüüğ"

ADDED: Swift2

let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"

var result = ""
str.uppercaseString.enumerateSubstringsInRange(str.characters.indices, options: .BySentences) { (sub, _, _, _)  in
    result += String(sub!.characters.prefix(1))
    result += String(sub!.characters.dropFirst(1)).lowercaseString
}

print(result)
like image 126
rintaro Avatar answered Oct 26 '25 06:10

rintaro


Updating @rintaro's code for Swift 3:

let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"

var result = ""
str.uppercased().enumerateSubstrings(in: str.startIndex..<str.endIndex, options: .bySentences) { (sub, _, _, _)  in
    result += String(sub!.characters.prefix(1))
    result += String(sub!.characters.dropFirst(1)).lowercased()
}

print(result)
like image 42
Katy Avatar answered Oct 26 '25 04:10

Katy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!