- //比较两个字符串内容是否相同
- BOOL b =[str1 isEqualToString:str2];
- // 比较两个字符串的大小
- NSComparisonResult result = [str1 compare:str2];
- //判断字符串是否以指定字符串开头
- [str1 hasPrefix:@"aaa"];
- //判断字符串是否以指定字符串结尾
- [str1 hasSuffix:@"aaa"];
- //字符串的截取
- NSString *str = @"123456789";
- NSLog(@"%@",[str substringFromIndex:3]);
- NSLog(@"%@",[str substringToIndex:6]);
- NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 3)]);
- //字符串转为 int double float
- NSString *str3 = @"123";
- NSLog(@"%i",[str3 intValue]);
- NSLog(@"%zi",[str3 length]);
-
- //取出指定位置的字符
- unichar c = [str3 characterAtIndex:2];
- NSLog(@"%c",c);
-
- //转为C语言的字符串
- const char *s = [str3 UTF8String];
- NSLog(@"%s",s);
- //字符串拆分为数组
- NSString *str9 = @"命 运 就 算 颠 沛 流 离";
- NSArray *array = [str9 componentsSeparatedByString:@" "];
- //拼接字符串
- NSString *str11 = @"123"; .
- NSString *str12 = @"456";
- NSString *appStr1 = [[NSString alloc]initWithFormat:@"%@%@",str11,str12];
- NSString *appStr2 = [str11 stringByAppendingFormat:@"%@",str12];
- NSString *appStr3 = [str11 stringByAppendingString:str12];
- //替换
- [mStr1 replaceCharactersInRange:NSMakeRange(4, 4) withString:@"1234"];
-
- //删除
- [mStr1 deleteCharactersInRange:NSMakeRange(4, 4)];
- NSLog(@"%@",mStr1);
- //插入
- NSMutableString *mStr1 = [[NSMutableString alloc] initWithFormat:@"abcd"];
- [mStr1 insertString:@"defg" atIndex:4];
复制代码
学习Foundation框架让大家都很头痛吧,命名没有什么知识点,但是却又一大堆复杂的方法要记,而且方法名还那么长。我总结了一下String中常用的方法给大家分享下。
|
|