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(4, 2)]; NSLog(@"str = %@",str2);
ie
1 NSMutableArray *array = [[NSMutableArray alloc] init]; 2 [array addObject:@"tom"]; 3 NSNumber *num = [NSNumber numberWithInt:12]; //装箱 4 [array addObject:num]; 5 [array addObject:[NSNumber numberWithFloat:3.5]]; 6 NSNumber *numobj=[array objectAtIndex:1]; 7 NSLog(@"%d",[numobj intValue]); //拆箱 8 9 //NSValue 10 NSRange range; 11 range.location =1; 12 range.length =20; 13 NSValue *value=[NSValue valueWithRange:range]; 14 NSLog(@"%@",value); 15 //NSNull 16 NSArray *arr = [NSArray arrayWithObjects:@"tom" ,[NSNull null],nil]; 17 for (int i =0; i<[arr count]; i++) { 18 if ([arr objectAtIndex:i]!=[NSNull null]) { 19 NSLog(@"%@",[arr objectAtIndex:i]); 20 } 21 else 22 { 23 NSLog(@"第%d个元素是空值",i); 24 } 25 } 26 //测试NSNull单例 27 NSNull *nl=[NSNull null]; 28 NSNull *nl2=[NSNull null]; 29 NSLog(@"%i",nl==nl2);//由于是单例所以地址相等,结果:1 30 NSArray *array1=[NSArray arrayWithObjects:@"abc",nl,@123, nil]; 31 NSLog(@"%@",array1);