如何将字符串中每个句子的第一个字母大写?我应该使用.capitalisedString
吗?
答案 0 :(得分:4)
您可以使用String
为每个句子枚举NSStringEnumerationOptions.BySentences
。但只有当第一个字符是大写字母时,它才会检测到“句子”。
所以,这可能不完美,但你可以试试这个:
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."
答案 1 :(得分:-1)
public static void main(String[] args) {
String a = "this is.a good boy";
String[] dot = a.split("\\.");
int i = 0;
String output = "";
while (i < dot.length) {
dot[i] = String.valueOf(dot[i].charAt(0)).toUpperCase()
+ dot[i].substring(1);
output = output + dot[i] + ".";
i++;
}
System.out.println(output);
}