如何使用 Swift 将每个句子中的第一个单词大写 [英] How to capitalize first word in every sentence with Swift

查看:88
本文介绍了如何使用 Swift 将每个句子中的第一个单词大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到用户语言环境,如何将段落中每个句子的第一个单词大写?我想要实现的是,无论句子中的大小写,每个单词的第一个字母都是大写的,其余的都是小写的.我只能做一个句子,首先将所有内容都转换为小写,然后获取第一个字母并制作大写,最后将它们加在一起.我的问题与 如何使用 Swift iOS 将字符串中的每个单词大写 不同,因为我不想将每个单词大写.我只想将每个句子的第一个单词大写.capitalizedString

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."

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

我想要的是

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

我的问题也不同于每个句子的首字母大写@rintaro 的代码在我下面的例子中不起作用.它保持原始文本中的大写字母完整.使用@rintaro 的代码;

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;

之前

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

之后

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

我想要达到的目标

"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]

如果您能仔细阅读我的问题,我将不胜感激,因为这是我第三次编辑问题.如果我不是母语人士,不能问清楚,我真的很抱歉.所以即使@rintaro 回答了类似的问题,他的回答也不能解决我的问题.@martin-r 提出了一个 Objective-C 的答案,这同样不能解决我的问题.还有另一个用户 eric something 也提出了另一个答案,但后来被删除了.我只是不明白为什么有几个人提出了不同的答案,但没有回答我的问题.

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.

推荐答案

您可以使用正则表达式来实现这一点.我将此函数添加为 String 扩展名,因此将来调用起来会很简单:

You can use Regular Expressions to achieve this. I'm adding this function as a String extension so it will be trivial to call in the future:

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()

这篇关于如何使用 Swift 将每个句子中的第一个单词大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆