一、字符串的初始化
1>最简单初始化 ————最常用
// NSString *str1 = @"I Love You!"; //常量区
2>常规初始化
// NSString *str2 = [[NSString alloc] initWithFormat:@"%@",@"I Heat You!"];
// NSString *str3 = [[NSString alloc] initWithFormat:str2];
//
便利构造初始化, ————最常用
// NSString *str4 =[NSString stringWithFormat:@"%d",123];
//
二、字符串的转换
NSString * str = @"hello";
const char *p = [str UTF8String];将OC字符串转化为C的字符串
str = @"123";
int a = [str intValue];//将数字串转化成整型数据
float a = [str floatValue];//将数字串转化成float型
double a = [str doubleValue];//将数字串转化成double型
三、字符串的拼接
NSString *str5 = [str1 stringByAppendingString:str2];//<span style="font-family:Menlo;">将str2拼接在str1后面</span>
NSString *str6 = [str1 stringByAppendingFormat:@",%d,%@",123,str2];
四、字符串的截取
NSString *str1 = @"123456";
NSString *str2 = [str1 substringFromIndex:2];//从第几个开始截取
NSString *str3 = [str1 substringToIndex:3];//截取到3 的字符串,
NSString *str4 = [str1 substringWithRange:NSMakeRange(1, 3)];//从下标为1的 长度为4的字符串
五、字符串的查找
NSString *str1 = @"1234Abcd";
NSRange range = [str1 rangeOfString:@"a"];<span style="font-family:Menlo;">//查找为“a”字符</span>
NSRange range2 =[str1 rangeOfString:@"a"options:NSCaseInsensitiveSearch];//
<p style="margin: 0px; color: rgb(0, 0, 0); font-family: sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family:Menlo;color:#08f0;"></span></p><pre name="code" class="objc">if (range2.length == 0 && range2.location == NSNotFound) {
// NSLog(@"没有找到");
// } else {
// NSLog(@"%d,%d",range2.location,range2.location);
// }
六、字符串的替换
strinByReplacing
NSString *str1= @"I Love You!";
// NSString *str2= [str1 stringByReplacingOccurrencesOfString:@"L" withString:@"U"];//将查到<span style="font-family:Menlo;">L 修改为U</span>
// NSLog(@"%@",str2);
七、字符串的比较
NSString *str1 = @"aA";
NSString *str2 = @"Aa";
if([str1 isEqualToString:str2] == YES){ //<span style="font-family:Menlo;">比较2个字符串,返回的结果是BOOL类型的值</span>
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
<span style="font-family:Menlo;">// 比较并排序</span>
NSComparisonResult result = [str1 compare:str2]; //比较2个字符串,<span style="font-family:Menlo;">根据ASCII码比较,根据返回值进行排序</span>
if (result ==NSOrderedSame) {//相等
NSLog(@"相等");
}else if (result == NSOrderedAscending){//升序
NSLog(@"升序");
}else{
NSLog(@"降序");
}
八、字符串的大小写转换
NSString *str = @"Say Hello!";
NSString *str2 = [str uppercaseString];//小写字母转大写
NSString *str3 = [str2 lowercaseString];//大写转小写
NSString *str4 = [str2 capitalizedString];//首字母大写
九、可变字符串的操作 NSMutableString
注意:可变字符串是继承至不可变字符串,因此具备父类的方法。
NSMutableString *str1 = [[NSMutableString alloc] initWithFormat:@"123"];
便利构造
NSMutableString *str2 = [NSMutableString stringWithFormat:@"qqe5"];
增加插入
// [str1 insertString:@"azc" atIndex:3];
// NSLog(@"%@",str1);
十、字符串的翻转
字符串的翻转
NSMutableString *s = [NSMutableString string];
for (NSUInteger i=self.length; i>0; i--) {
[s appendString:[self substringWithRange:NSMakeRange(i-1, 1)]];
}
return s;