NSString *info=@"Hello world";
NSString *info=[[NSString alloc]initWithFormat:@"my name is %@",name];
将char 类型的字符串和 NSString 进行转换
char *cStr = "i am c string";
NSString *str=[[NSString alloc] initWithCString: cStr encoding: (编码方法通常是utf8)];
NSString *str2=@"hello";
char *cstr2 = [str2 UTF8String];
使用快捷方法进行字符串创建和初始化,stringWith开头方法
此种方法创建的字符串对象在非ARC环境下不需要释放
NSString *name =@"tom";
NSString *info=[NSString stringWithFormat:@"my name is %@",name];字符串的大小比较 compare方法
NSString *str1 =@"tom";
NSString *str2 = @"jack";
[str1 compare:str2];
NString *str1 = @"tom";
NString *str2 = @"jack";
NSComparisonResult result = [str1 compare:str2];
if(result==NSOrderedAscending)
{
NSLog(@"str1<str2");
}else if(result==NSOrdereDescending)
{
NSLog(@"str1>str2");
}else NSLog(@"str1==str2");
字符串是否相等[username isEqualToString:@"admin"]
字符串长度
[username length];
寻找子串rangeOfString返回类型NSRange 不存在时location 为NSNotFound
NSString *str = @"hello world";
NSRange range=[str rangeOfString:@"lloc"];可以查看NSRange的类型
子串的截取
字符串和文件以及文件路径,writeToFile,initWithContentsOfFele
NSString *str =@"i am a string";
BOOL result=[str writeToFile:@"/Users/administator/Desktop/1.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *content=[[NSString alloc]initWithContentsOfFile:@"/Users/administator/Desktop/1.txt",encoding:NSUTF8StringEncoding error:nil];
NSMutableString 是 NSString 的子类
NSMUtableString *str=[NSMutableString stringWithCapacity:20];
[str appendString:@"123"]; str后面加上123字符串
[str insertString:@"age is " atIndex:0];插入
[str deleteCharactersInRange:NSMakeRange(4,2)];删除
[str replaceCharactersInRange:NSMakeRange(5,2) withString:@"22"];替换
[str setString:;@"22"]; 重置
//一.不变字符串 //1.常量字符串,比较字符串
// NSString *str1 = @"hello world";
// NSString *str2 = @"hello world";
// if (str1==str2) {
// NSLog(@"equal");
// }
// else
// {
// NSLog(@"unequal");
// }
// NSLog(@"str1 address is %p",str1);
// NSLog(@"str2 address is %p",str2);
//使用init方法进行创建
// NSString *name = @"tom";
// NSString *info = [[NSString alloc] initWithFormat:@"my name is %@",name];
// NSLog(@"%@",info);
//2.使用快捷方法进行字符串的创建和初始化
// NSString *name = @"tom";
// NSString *info = [NSString stringWithFormat:@"my name is %@",name];
// NSLog(@"%@",info);
//3.大小写转换及前后缀
// NSLog(@"\"Hello world!\" to upper is %@",[@"Hello world!" uppercaseString]);
// NSLog(@"\"Hello world!\" to lowwer is %@",[@"Hello world!" lowercaseString]);
//前缀
// NSLog(@"has prefix ab? %i",[@"abcdef" hasPrefix:@"abd"]);
// //后缀
// NSLog(@"has suffix ab? %i",[@"abcdef" hasSuffix:@"ef"]);
//4.字符串的比较
// NSString *str1 = @"tom";
// NSString *str2 = @"eric";
// NSComparisonResult result= [str1 compare:str2];
// if (result==NSOrderedAscending) {
// NSLog(@"str1<str2");
// }
// else if (result ==NSOrderedDescending)
// {
// NSLog(@"str1>str2");
// }
// else
// {
// NSLog(@"str1==str2");
// } //5.char类型和NSString类型转换
// char *str= "test case";
// NSString *str3= [[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding];
// char *str4 = (char *)[str3 UTF8String];
// NSLog(@"%s",str4);
//6.字符串值的比较
// NSString *username = @"admin";
// NSString *password = @"123456";
// if ([username isEqualToString:@"admin"] && [password isEqualToString:@"123456"]) {
// NSLog(@"值相等");
// }
// else
// {
// NSLog(@"值不相等");
// }
// 7.字符串的长度
// NSString *str =@"alfred";
// if ([str length]>=6) {
// NSLog(@"the length is %lu",[str length]);
// }
// else
// {
// NSLog(@"ok");
// }
//8.字符串截取
// NSLog(@"%@",[@"abcdef" substringFromIndex:3]);//从第三个索引开始(包括第三个索引对应的字符)截取到最后一位
// //结果:def
// NSLog(@"%@",[@"abcdef" substringToIndex:3]);////从0开始截取到第三个索引(不包括第三个索引对应的字符)
// //结果:abc
// NSLog(@"%@",[@"abcdef" substringWithRange:NSMakeRange(2, 3)]); //9.数值转换
// NSString *value1 = @"10";
// NSString *value2 = @"3.6";
// float sum = [value1 intValue] +[value2 floatValue];
// NSLog(@"%0.2f",sum);
//10.文件相关
// NSString *str =@"i am a string abc";
// [str writeToFile:@"/Users/admin/Desktop/test2.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
// NSString * str2 = [[NSString alloc] initWithContentsOfFile:@"/Users/admin/Desktop/test2.txt" encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@",str2);
//NSString *str3 = [NSString alloc]
// //二.可变字符串 //NSMutableString *str = [NSMutableString stringWithCapacity:20];
NSMutableString *str2 = [[NSMutableString alloc]init];
[str2 setString:@"test case"];
// [str appendString:@"12"];
// NSString *name = @"tom";
// [str appendFormat:@",name is %@",name];
// [str replaceCharactersInRange:NSMakeRange(4, 2) withString:@"22"];
// [str insertString:@"it is" atIndex:3];
[str2 deleteCharactersInRange:NSMakeRange(, )];
NSLog(@"str = %@",str2);
ie
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"tom"];
NSNumber *num = [NSNumber numberWithInt:]; //装箱
[array addObject:num];
[array addObject:[NSNumber numberWithFloat:3.5]];
NSNumber *numobj=[array objectAtIndex:];
NSLog(@"%d",[numobj intValue]); //拆箱 //NSValue
NSRange range;
range.location =;
range.length =;
NSValue *value=[NSValue valueWithRange:range];
NSLog(@"%@",value);
//NSNull
NSArray *arr = [NSArray arrayWithObjects:@"tom" ,[NSNull null],nil];
for (int i =; i<[arr count]; i++) {
if ([arr objectAtIndex:i]!=[NSNull null]) {
NSLog(@"%@",[arr objectAtIndex:i]);
}
else
{
NSLog(@"第%d个元素是空值",i);
}
}
//测试NSNull单例
NSNull *nl=[NSNull null];
NSNull *nl2=[NSNull null];
NSLog(@"%i",nl==nl2);//由于是单例所以地址相等,结果:1
NSArray *array1=[NSArray arrayWithObjects:@"abc",nl,@, nil];
NSLog(@"%@",array1);