设置“你好,世界”中的文本格式。你好吗?到“你好,世界。你好吗?在 ObjC(iOS) 中?
正如标题所说,我需要格式化一串文本,格式如下:“HELLO,WORLD。HOW ARE YOU?”进入“你好,世界。你好吗?”,在iOS中有没有标准的方法可以做到这一点?或者有示例代码吗?
谢谢!
As the title said, I need to format a string of text in format like: "HELLO, WORLD. HOW ARE YOU?" into "Hello, world. How are you?", is there any standard method for doing this in iOS? Or is there any sample code ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道执行此操作的标准方法,而且我认为没有标准方法。
但是
NSString
提供了这样的方法:-(NSString *)capitalizedString;
它返回一个新字符串,每个单词的第一个字符在上案件。正确折断绳子后,您就可以使用它了。还可以考虑使用以下方式获取小写字符串:-(NSString *)lowercaseString
。I don't know standard method to do this, and I don't think there is one.
But the
NSString
provides methods such :-(NSString *)capitalizedString;
which returns a new string, with the first character of each word in upper case. After breaking correctly your string you could use it. Also think of getting a string in lower case using :-(NSString *)lowercaseString
.没有直接的方法可以将段落中句子的第一个字符大写。 NSString 只有一个方法capitalizedString,它可以将每个单词大写。您必须创建自己的逻辑才能实现这一目标。您可以尝试以下代码。
注意:上面的代码假设段落中的所有句子都以字符
". "
(一个点和一个空格)结尾,除了最后一个句子(它可以以任何字符结尾)特点)。如果句子以"? "
或"! "
等其他字符结尾,则此方法将返回不完全格式化的字符串。There is no direct way to capitalize the first characters of sentences in a paragraph. NSString just has a method capitalizedString which capitalizes each and every word. You have to create your own logic to achieve this. You can try the following code.
Note: The above code assumes that all the sentences in the paragraph ends with characters
". "
(a dot and a space) except the last sentence(it can end with any character). If a sentence ends with some other characters like"? "
or"! "
, then this method will return a not-fully-formatted string.假设标点符号后的 2 个字符是下一个句子的第一个字符,则此方法有效。
Assuming that 2 characters after the punctuation is the first character of the next sentence, this works.