IOS字符串(NSString)的常用用法
//创建一个空的字符串
NSString*string1=[[NSStringalloc]init];
NSString*string2=[NSStringstring];
NSLog(@"string1=%@",string1);
NSLog(@"string2=%@",string2);
//创建一个字符串常量
NSString*string3=@"Hello World";
NSLog(@"string3=%@,%p",string3,string3);
//根据已有的字符串创建新的字符串(常量区)
NSString*string4_1=[[NSStringalloc]initWithString:string3];
NSLog(@"string4_1:%@,%p",string4_1,string4_1);
NSString*string4_2=[NSStringstringWithString:string3];
NSLog(@"string4_2:%@,%p",string4_2,string4_2);
//根据已有的字符串创建新的字符串(堆区)
NSString*string5_1=[[NSStringalloc]initWithFormat:@"%@",string3];
NSLog(@"string5_1:%@,%p",string5_1,string5_1);
NSString*string5_2=[NSStringstringWithFormat:@"%@",string3];
NSLog(@"string5_2:%@,%p",string5_2,string5_2);
//字符串的比较
if(string4_1==string4_2)//指针的比较
{
NSLog(@"相同指针");
}
if([string5_1isEqualToString:string5_2])//内容比较
{
NSLog(@"相同内容");
}
else
NSLog(@"不同内容");
BOOLisSame=[string5_1isEqualToString:string5_2];//内容比较,相同语句
if(isSame) {
NSLog(@"内容相同");
}
else
NSLog(@"内容不同");
//字符串转其他类型
NSString*string6_1=@"3.1415926";
floatpi=[string6_1doubleValue];//双精度浮点
NSLog(@"string6->float:%f",pi);
NSString*string6_2=@"1000.9";
intaInt=[string6_2intValue];//整型
NSLog(@"string6->integer:%d",aInt);
NSString*string6_3=@"Y";
BOOLaBool=[string6_3boolValue];//布尔类型
NSLog(@"string6->bool:%d",aBool);
//字符串长度
NSString*string7=@"This-is-a-string";
NSLog(@"%ld",[string7length]);//等同于string7.length
//截取字符串:根据已有的字符串,返回新的子字符串(截取其中某些位置的字符串)
NSString*substring1=[string7substringFromIndex:4];//从第几位开始截取
NSString*substring2=[string7substringToIndex:6];//截取到第几位
NSString*substring3=[string7substringWithRange:NSMakeRange(5,4)];//选取某一区域截取
NSRangerange={5,4};
range.location=6;//后期修改位置
range.length=3;//后期修改截取长度
NSString*substring4=[string7substringWithRange:range];//选择某一区域截取
NSLog(@"substring1:%@",substring1);
NSLog(@"substring2:%@",substring2);
NSLog(@"substring3:%@",substring3);
NSLog(@"substring4:%@",substring4);
for(inti=0; i<[string7length]; i++) {
NSLog(@"substing:%@",[string7substringWithRange:NSMakeRange(i,1)]);
}
//按照字符串中的某个字符转成数组
NSArray*array1=[string7componentsSeparatedByString:@"-"];//根据“-”分成数组
NSLog(@"string->array:%@",array1);
NSString*string8=@"THiS iS a String";
NSLog(@"%@",[string8uppercaseString]);//大写(不改变String8)
NSLog(@"%@",[string8lowercaseString]);//小写(不改变String8)
NSLog(@"%@",[string8capitalizedString]);//首字母大写(不改变String8)
//比较字符串的大小
NSString*string9_1=@"abc";
NSString*string9_2=@"xyz";
NSComparisonResultresult =[string9_1caseInsensitiveCompare:string9_2];//NSComparisonResult属于枚举
NSLog(@"");
switch(result) {
caseNSOrderedAscending:
NSLog(@"9_1小");
break;
caseNSOrderedSame:
NSLog(@"9_1等于");
break;
caseNSOrderedDescending:
NSLog(@"9_1大");
break;
default:
break;
}
//字符串的查找
NSString*string10=@"abcdefghijklmnopq";
NSRangerange1=[string10rangeOfString:@"hijk"];
if(range1.length>0) {
NSLog(@"找到了");
}
NSLog(@"%ld,%ld",range1.length,range1.location);//第七位,长度为4
NSLog(@"%@",NSStringFromRange(range1));
NSLog(@"%@",NSStringFromClass([NSStringclass]));
// NSStringFromClass([NSString class]);
//判断字符串是不是以*字符串开头
BOOLisPre=[string10hasPrefix:@"abc"];
if(isPre) {
NSLog(@"是以abc开头");
}
//判断字符串是不是以*字符串结尾
BOOLisSuf=[string10hasSuffix:@"opq"];
if(isSuf) {
NSLog(@"是以opq结尾");
}
//字符串的拼接
NSString*string11_1=@"good";
NSString*string11_2=@"moring";
NSString*string11=[NSStringstringWithFormat:@"%@,%@",string11_1,string11_2];
NSLog(@"string11:%@",string11);
NSString*string12=[string11_1stringByAppendingString:string11_2];//最常用
NSLog(@"string12:%@",string12);
//子类复用父类成员变量、属性、方法
/***************************NSMutableString*****************************/
//NSMutableString是NSString的子类(继承关系)
NSMutableString*mutableString1=[NSMutableStringstringWithFormat:@"HelloWorld"];
NSLog(@"muatbleString2:%@",mutableString1);
//插入,第一个参数表示需要插入的字符串,第二个参数是放置的位置
[mutableString1insertString:@"_ios"atIndex:[mutableString1length]];
NSLog(@"mutableString:%@",mutableString1);
//替换,第一个参数表示需要替换的位置,第二个参数是要替换的字符串
[mutableString1replaceCharactersInRange:NSMakeRange([mutableString1length]-3,3)withString:@"apple"];
NSLog(@"mutableString1:%@",mutableString1);
//删除
NSRangedeleteRange=NSMakeRange([mutableString1length]-6,6);
[mutableString1deleteCharactersInRange:deleteRange];
NSLog(@"mutableString1:%@",mutableString1);
//查找->使用不可变的rangeOfString
#prama Mark -判断字符串包含另一个字符串的内容
if([string1rangeOfString:string2].location!=NSNotFound) {
//string1包含string2的内容
DoSomething…
}
//去除字符串的最后一位(随意减)
NSString *cccc = [city substringToIndex:[city length] - 1];
百分号在objc中是%%,
例如:
NSString *criteria = [NSString stringWithFormat:@"WHERE username LIKE '%@%%'",name];
//添加行间距
- (void)setHotSpotData:(NSDictionary*)dic{
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStylealloc]init];
[paragraphStylesetLineSpacing:8];
UIColor*color = [UIColorblackColor];
NSAttributedString*string = [[NSAttributedStringalloc]initWithString:dic[@"content1"]attributes:@{NSForegroundColorAttributeName: color,NSParagraphStyleAttributeName: paragraphStyle}];
self.content1.attributedText= string;
// self.content1.text = dic[@"content1"];
}