考虑到用户区域设置,如何将段落中每个句子的第一个单词大写?我想要实现的是,无论句子中的大小写,每个单词的第一个字母都是大写,其余的都是小写。我只能做一个句子,首先将所有内容都转换为小写,然后获取第一个字母并将其变为大写,最后将它们加在一起。我的问题与如何使用 Swift iOS 将字符串中的每个单词大写不同,因为我不想将每个单词大写。我只想将每个句子的第一个单词大写。 capitalizedString
转
"this is first sentence. this is second sentence."
到
"This Is First Sentence. This Is Second Sentence."
我想要的是
"This is first sentence. This is second sentence."
我的问题也不同于
大写每个句子的第一个字母因为@rintaro的代码不适用于我下面的示例。它完整保留了原文中的大写字母。使用@rintaro的代码;
之前
"someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
之后
"SomeSentenceWith UTF text İŞğĞ. AnotherSentenceğüÜğ."
我想要实现的目标,
"Somesentencewith utf text işğğ. Anothersentenceğüüğ."
我下面的代码只能进行部分转换。
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]
如果您能仔细阅读我的问题,我将非常感激,因为这是我第三次编辑问题。如果我不能问清楚,我真的很抱歉,因为我不是母语人士。因此,尽管@rintaro 回答了类似的问题,但他的回答并没有解决我的问题。 @martin-r 建议了一个 Objective-C 答案,但它又不能解决我遇到的问题。还有另一个用户 eric Something 也提出了另一个答案,但后来被删除了。我只是不明白为什么有几个人提出了不同的答案,而这并不能回答我的问题。
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ğüüğ"
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)
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)
extension String {
func toUppercaseAtSentenceBoundary() -> String {
var string = self.lowercaseString
var capacity = string.utf16Count
var mutable = NSMutableString(capacity: capacity)
mutable.appendString(string)
var error: NSError?
if let regex = NSRegularExpression(
pattern: "(?:^|\\b\\.[ ]*)(\\p{Ll})",
options: NSRegularExpressionOptions.AnchorsMatchLines,
error: &error
) {
if let results = regex.matchesInString(
string,
options: NSMatchingOptions.allZeros,
range: NSMakeRange(0, capacity)
) as? [NSTextCheckingResult] {
for result in results {
let numRanges = result.numberOfRanges
if numRanges >= 1 {
for i in 1..<numRanges {
let range = result.rangeAtIndex(i)
let substring = mutable.substringWithRange(range)
mutable.replaceCharactersInRange(range, withString: substring.uppercaseString)
}
}
}
}
}
return mutable
}
}
var string = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ.".toUppercaseAtSentenceBoundary()
扩展字符串 {
var toUppercaseAtSentenceBoundary: String {
var result = ""
self.enumerateSubstrings(in: startIndex..., options: .bySentences) { sub, _, _, _ in
result += String(sub!.prefix(1)).uppercased()
result += String(sub!.dropFirst(1)).lowercased()
}
return result
}
}使用方法:
let string = "This is First sentence. This is second Sentence.".toUppercaseAtSentenceBoundary
print(string) /* Output: "This is first sentence. This is second sentence." */