objective-c – capitalizedString没有正确地大写以数字开头的单词?

objective-c – capitalizedString没有正确地大写以数字开头的单词?,第1张

概述我正在使用NSString方法[myString capitalizedString]来大写我的字符串中的所有单词. 但是,对于以数字开头的单词,大小写不能很好地工作. i.e. 2nd chance 变 2Nd Chance 即使n不是单词的第一个字母. 谢谢 你必须为这个问题推出自己的解决方案. Apple docs声明您可能无法使用该函数为多字符串和具有特殊字符的字符串获取指定的行为.这是一 我正在使用Nsstring方法[myString cAPItalizedString]来大写我的字符串中的所有单词.

但是,对于以数字开头的单词,大小写不能很好地工作.

i.e. 2nd chance

2Nd Chance

即使n不是单词的第一个字母.

谢谢

解决方法 你必须为这个问题推出自己的解决方案. Apple docs声明您可能无法使用该函数为多字符串和具有特殊字符的字符串获取指定的行为.这是一个非常粗糙的解决方案

Nsstring *text = @"2nd place is nothing";// break the string into words by separating on spaces.NSArray *words = [text componentsSeparatedByString:@" "];// create a new array to hold the cAPItalized versions.NSMutableArray *newWords = [[NSMutableArray alloc]init];// we want to ignore words starting with numbers.// This class helps us to determine if a string is a number.NSNumberFormatter *num = [[NSNumberFormatter alloc]init];for (Nsstring *item in words) {    Nsstring *word = item;     // if the first letter of the word is not a number (numberFromString returns nil)    if ([num numberFromString:[item substringWithRange:NSMakeRange(0,1)]] == nil) {        word = [item cAPItalizedString]; // cAPItalize that word.    }     // if it is a number,don't change the word (this is implIEd).    [newWords addobject:word]; // add the word to the new List.}NSLog(@"%@",[newWords description]);
总结

以上是内存溢出为你收集整理的objective-c – capitalizedString没有正确地大写以数字开头的单词?全部内容,希望文章能够帮你解决objective-c – capitalizedString没有正确地大写以数字开头的单词?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003645.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存