每个句子的首字母大写 [英] Capitalise first letter of every sentence

查看:110
本文介绍了每个句子的首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串中每个句子的第一个字母大写?我应该使用 .capitalisedString 吗?

How do I capitalise the first letter of every sentence in a string? Should I use .capitalisedString?

推荐答案

您可以使用 NSStringEnumerationOptions.BySentences 为每个句子枚举 String.但只有第一个字符是大写的,它才会检测到句子".

You can enumerate String per sentences by using NSStringEnumerationOptions.BySentences. But it detect a "sentence" only if the first character is upper-cased.

所以,这可能并不完美,但你可以试试这个:

So, This may not be perfect, but you can try this:

import Foundation

let text:String = "lorem ipsum dolor elit, sed aliqfuas. imfs enim ad veniam, quis nostrud consequat? duis aute irure dolor in pariatur."

var result = ""
text.uppercaseString.enumerateSubstringsInRange(text.startIndex..<text.endIndex, options: .BySentences) { (_, range, _, _) in
//  ^^^^^^^^^^^^^^^^ enumerate all upper cased string

    var substring = text[range] // retrieve substring from original string

    let first = substring.removeAtIndex(substring.startIndex)
    result += String(first).uppercaseString + substring
}

// result -> "Lorem ipsum dolor elit, sed aliqfuas. Imfs enim ad veniam, quis nostrud consequat? Duis aute irure dolor in pariatur."

这篇关于每个句子的首字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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