可变字符串
//插入字符串
NSMutableString *string1 =[NSMutableString stringWithString:@"字符串"];
[string1 insertString:@"可变" atIndex:0];//在string的第0个位置插入
NSLog(@"string = %@",string1);//可变字符串
//删除字符串
NSMutableString *string2 = [NSMutableString stringWithString:@"aabb字符串"];
NSRange range = NSMakeRange(0, 4);//指定范围 第一个位置,长度为4
[string2 deleteCharactersInRange:range];//删除指定范围的字符串
NSLog(@"string2 = %@",string2);//字符串
//追加字符串
NSMutableString *string3 = [NSMutableString stringWithString:@"字符串"];
[string3 appendString:@"追加"];
NSLog(@"string3 = %@",string3);//追加字符串
字符串
//比较字符串内容是否相同
NSString *s1=[NSString stringWithFormat:@"字符串"];
NSString *s2=[NSString stringWithFormat:@"字符串"];
//比较的是指针地址
if (s1 == s2) {
NSLog(@"s1== s2");
}
else
NSLog(@"s1 != s2");//s1 != s2
//比较字符串的内容
bool isSame =[s1 isEqualToString:s2];
if (isSame) {
NSLog(@"s1 == s2");
}
else
NSLog(@"s1 != s2");//s1 == s2
//比较字符串大小
NSString *n1=[NSString stringWithFormat:@"a"];
NSString *n2=[NSString stringWithFormat:@"b"];
NSComparisonResult result = [n1 caseInsensitiveCompare:n2];
if (result == NSOrderedAscending) {
NSLog(@"升序:a<b");
}
else if (result == NSOrderedSame){
NSLog(@"内容相同");
}
else if (result == NSOrderedDescending){
NSLog(@"降序: a>b");
}//a<b
//访问字符串长度
NSString *nns = @"HWELWE";
NSUInteger len = [nns length];
NSLog(@"len is %ld",len);//6
//大小写转换
NSString *hello = @"hello WORLD";
NSLog(@"%@",[hello uppercaseString]);//全部大写//HELLO WORLD
NSLog(@"%@",[hello lowercaseString]);//全部小写//hello world
NSLog(@"%@",[hello capitalizedString]);//首字母大写,其他字母小写//Hello World
//转换为基本数据类型
NSString *st1 =@"10";
int a =[st1 intValue];
NSLog(@"a = %d",a);//10
NSString *st2 =@"10.23";
float b =[st2 floatValue];
NSLog(@"b = %.2f",b);//10.23
NSString *st3 =@"1";
BOOL c = [st3 boolValue];
NSLog(@"c = %d ",c);//1
//分割字符串
NSString *str = @"asfs-dfgf-sgfds,fdsa";
NSArray *arr = [str componentsSeparatedByString:@"-"];
NSLog(@"%@",arr);
//访问指定位置字符
NSString *string = @"abcdefg";
char s = [string characterAtIndex:3];
NSLog(@"s = %c",s);//d
//截取字符串
NSString *ss = @"abcdef";
//截取到指定位置
NSString *string1 = [ss substringToIndex:3];
NSLog(@"string1 = %@",string1);//abc
//从指定位置截取到末尾
NSString *string2 =[ss substringFromIndex:2];
NSLog(@"string2 = %@",string2);
//指定范围截取
NSRange range = NSMakeRange(2, 3);//从下标2开始截取3位
NSString *string3 = [ss substringWithRange:range];
NSLog(@"string3 = %@",string3);
//拼接字符串
NSString *ns1 = @"abx";
NSString *ns2 = @"ycd";
NSString *strr1 = [NSString stringWithFormat:@"字符串1:%@,字符串2:%@",ns1,ns2];//字符串1:abx,字符串2:ycd
NSLog(@"strr = %@",strr1);
NSString *strr2 = [ns1 stringByAppendingString:ns2];
NSLog(@"strr2 = %@",strr2);//abxycd
NSString *strr3 = [ns1 stringByAppendingFormat:@"追加字符串2:%@",ns2];
NSLog(@"strr3 = %@",strr3);//abx追加字符串2:ycd
//查找字符串
NSString *email = @"123456@qq.com";
NSString *substring = @"@qq.com";
NSRange range1 = [email rangeOfString:substring];
if (range1.location != NSNotFound) {
NSLog(@"%@包含%@",email ,substring);
NSLog(@"位置是:%ld,长度是%ld",range1.location,range1.length);
}
else
{
NSLog(@"没找到");
}//位置是:6,长度是7
//替换字符串
NSString *newemail = [email stringByReplacingOccurrencesOfString:@"qq.com" withString:@"163.com"];
NSLog(@"new email = %@",newemail);//new email = 123456@163.com