iOS 字符串中的字母大小写转换、首字母大写转换

1. NSString 提供的API如下:

一般的英语转换用下面这仨就行:

/* The following three return the canonical (non-localized) mappings. They are suitable for programming operations that require stable results not depending on the user's locale preference.  For locale-aware case mapping for strings presented to users, use the "localized" methods below.
*/
// 全员转大写
@property (readonly, copy) NSString *uppercaseString;
// 全员转小写
@property (readonly, copy) NSString *lowercaseString;
// 首字母
@property (readonly, copy) NSString *capitalizedString;

需要考虑其他语言要做本地化的转换时,才需要下面这几个,比如德语法语啥的,大小写不一定对应一样的字符个数;

/* The following three return the locale-aware case mappings. They are suitable for strings presented to the user.
*/
@property (readonly, copy) NSString *localizedUppercaseString API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSString *localizedLowercaseString API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSString *localizedCapitalizedString API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0));

/* The following methods perform localized case mappings based on the locale specified. Passing nil indicates the canonical mapping.  For the user preference locale setting, specify +[NSLocale currentLocale].
*/
- (NSString *)uppercaseStringWithLocale:(nullable NSLocale *)locale API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
- (NSString *)lowercaseStringWithLocale:(nullable NSLocale *)locale API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
- (NSString *)capitalizedStringWithLocale:(nullable NSLocale *)locale API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));

2. 举例

NSString *rpTestStr = @"aaBBccDDeeFF";

// transStr = @"AABBCCDDEEFF"
NSString *transStr = [rpTestStr uppercaseString]; 
  
// transStr = @"aabbccddeeff"
NSString *transStr = [rpTestStr lowercaseString];  
 
// transStr = @"Aabbccddeeff"
NSString *transStr = [rpTestStr capitalizedString]; 

你可能感兴趣的:(ios,NSString,字符串,字母大小写转换,首字母大写,驼峰,iOS,字符串)