NSString的比较专栏

 

先做个笔记不做具体说明,有空了再补上

复制代码
NSString *str;
// 使用stringWithFormat生成一格式化字符串
str = [NSString stringWithFormat:@"This is %@","John"];
NSLog(@"str--->%@",str);
// 字符串长度length;
NSLog(@"The length of this string is %@",[str length]);
// 字符串比较 isEqualToString, 返回NO(false),isEqualToString区分大小写
BOOL isequal = [str isEqualToString:@"this is John"];
 
// 字符串序列比列 compare,返回结果NSComparisonResult
// type enum _NSComparisonResult{
// NSOrderedAscending = -1,
// NSOrderedSame,
// NSOrderedDescending
// }
int result = [@"bool" compare:@"cool"];
NSLog(@"The result is %d",result);
 
// compare 比较规则options
// NSLiteralSearch 区分大小写(完全比较)
// NSCaseInsensitiveSearch 不区分大小写
// NSNumericSearch 只比较字符串的个数,而不比较字符串的字面值
int result1 = [@"This is John" compare:@"this is John" options:NSCaseInsensitiveSearch | NSNumericSearch];
NSLog(@"The result is %d",result1);
 
// 字符串开头是否包括另一字符串 hasPrefix,返回结果YES(true)
BOOL isHas = [str hasPrefix:@"This"];
// 字符串结尾是否包括另一字符串 hasSuffix,返回结果YES(true)
BOOL isHas = [str hasSuffix:@"John"];
 
// 查找字符串在另一字符串中的位置
NSRange range = [str rangeOfString:@"is" options:NSCaseInsensitiveSearch];
NSLog(@"The location in the string named 'str' of 'is' is @d",range.location);
复制代码

 

posted @   #零下一度&  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示