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]; 

相关推荐

  1. iOS 字符串字母大小写转换字母大写转换

    2024-01-18 13:48:01       53 阅读
  2. 【Python大写字符串每一个单词字母

    2024-01-18 13:48:01       22 阅读
  3. 字母大小写转换

    2024-01-18 13:48:01       21 阅读
  4. 将一串字符串小写字母转换大写

    2024-01-18 13:48:01       54 阅读
  5. C语言大小写字母转化

    2024-01-18 13:48:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 13:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 13:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 13:48:01       82 阅读
  4. Python语言-面向对象

    2024-01-18 13:48:01       91 阅读

热门阅读

  1. linux 内核ARM32启动

    2024-01-18 13:48:01       50 阅读
  2. idea个人常用快捷键汇总

    2024-01-18 13:48:01       60 阅读
  3. 去除GIT某个时间之前的提交日志

    2024-01-18 13:48:01       52 阅读
  4. ElasticSearch设置用户名密码访问

    2024-01-18 13:48:01       49 阅读
  5. vue2上传图片image-conversion压缩

    2024-01-18 13:48:01       51 阅读
  6. 2. FPGA的电路结构概述

    2024-01-18 13:48:01       57 阅读
  7. AI的能力上限是使用者的认知

    2024-01-18 13:48:01       57 阅读
  8. Django的后台认证登录系统,登录多久之后失效呢

    2024-01-18 13:48:01       62 阅读