可变字符串
//插入字符串
NSMutableString*string1 =[NSMutableStringstringWithString:@"字符串"];
[string1insertString:@"可变"atIndex:0];//在string的第0个位置插入
NSLog(@"string = %@",string1);//可变字符串
//删除字符串
NSMutableString*string2 = [NSMutableStringstringWithString:@"aabb字符串"];
NSRangerange =NSMakeRange(0, 4);//指定范围第一个位置,长度为4
[string2deleteCharactersInRange:range];//删除指定范围的字符串
NSLog(@"string2 = %@",string2);//字符串
//追加字符串
NSMutableString*string3 = [NSMutableStringstringWithString:@"字符串"];
[string3appendString:@"追加"];
NSLog(@"string3 = %@",string3);//追加字符串
字符串
//比较字符串内容是否相同
NSString*s1=[NSStringstringWithFormat:@"字符串"];
NSString*s2=[NSStringstringWithFormat:@"字符串"];
//比较的是指针地址
if(s1 == s2) {
NSLog(@"s1== s2");
}
else
NSLog(@"s1 != s2");//s1 != s2
//比较字符串的内容
boolisSame =[s1isEqualToString:s2];
if(isSame) {
NSLog(@"s1 == s2");
}
else
NSLog(@"s1 != s2");//s1 == s2
//比较字符串大小
NSString*n1=[NSStringstringWithFormat:@"a"];
NSString*n2=[NSStringstringWithFormat:@"b"];
NSComparisonResultresult = [n1caseInsensitiveCompare:n2];
if(result ==NSOrderedAscending) {
NSLog(@"升序:a
}
elseif(result ==NSOrderedSame){
NSLog(@"内容相同");
}
elseif(result ==NSOrderedDescending){
NSLog(@"降序: a>b");
}//a
//访问字符串长度
NSString*nns =@"HWELWE";
NSUIntegerlen = [nnslength];
NSLog(@"len is %ld",len);//6
//大小写转换
NSString*hello =@"hello WORLD";
NSLog(@"%@",[hellouppercaseString]);//全部大写//HELLO WORLD
NSLog(@"%@",[hellolowercaseString]);//全部小写//hello world
NSLog(@"%@",[hellocapitalizedString]);//首字母大写,其他字母小写//Hello World
//转换为基本数据类型
NSString*st1 =@"10";
inta =[st1intValue];
NSLog(@"a = %d",a);//10
NSString*st2 =@"10.23";
floatb =[st2floatValue];
NSLog(@"b = %.2f",b);//10.23
NSString*st3 =@"1";
BOOLc = [st3boolValue];
NSLog(@"c = %d ",c);//1
//分割字符串
NSString*str =@"asfs-dfgf-sgfds,fdsa";
NSArray*arr = [strcomponentsSeparatedByString:@"-"];
NSLog(@"%@",arr);
//访问指定位置字符
NSString*string =@"abcdefg";
chars = [stringcharacterAtIndex:3];
NSLog(@"s = %c",s);//d
//截取字符串
NSString*ss =@"abcdef";
//截取到指定位置
NSString*string1 = [sssubstringToIndex:3];
NSLog(@"string1 = %@",string1);//abc
//从指定位置截取到末尾
NSString*string2 =[sssubstringFromIndex:2];
NSLog(@"string2 = %@",string2);
//指定范围截取
NSRangerange =NSMakeRange(2, 3);//从下标2开始截取3位
NSString*string3 = [sssubstringWithRange:range];
NSLog(@"string3 = %@",string3);
//拼接字符串
NSString*ns1 =@"abx";
NSString*ns2 =@"ycd";
NSString*strr1 = [NSStringstringWithFormat:@"字符串1:%@,字符串2:%@",ns1,ns2];//字符串1:abx,字符串2:ycd
NSLog(@"strr = %@",strr1);
NSString*strr2 = [ns1stringByAppendingString:ns2];
NSLog(@"strr2 = %@",strr2);//abxycd
NSString*strr3 = [ns1stringByAppendingFormat:@"追加字符串2:%@",ns2];
NSLog(@"strr3 = %@",strr3);//abx追加字符串2:ycd
//查找字符串
NSString*email =@"123456@qq.com";
NSString*substring =@"@qq.com";
NSRangerange1 = [emailrangeOfString:substring];
if(range1.location!=NSNotFound) {
NSLog(@"%@包含%@",email ,substring);
NSLog(@"位置是:%ld,长度是%ld",range1.location,range1.length);
}
else
{
NSLog(@"没找到");
}//位置是:6,长度是7
//替换字符串
NSString*newemail = [emailstringByReplacingOccurrencesOfString:@"qq.com"withString:@"163.com"];
NSLog(@"new email = %@",newemail);//new email =123456@163.com