在学习c之后再来学习oc的一些类型,发现oc的类型真心的简便快捷.
下面就NSString的几点用法介绍一下吧.
1,字符串的创建和快速创建.
NSString *str = [[NSString alloc]init];
NSString *str1 = [[NSString alloc]initWithFormat:@"lichan%d",2];
NSString *str2 = [[NSString alloc]initWithUTF8String:const char*str ];
//我发现了NSString 没有中断输入函数,所以通过c创建char*,然后转换,很不错嘛
// 当然还有很多用法了.
//2)快速创建字符串
NSString *str4 = @"pk";
//NSString *str4 = [[NSString alloc] initWithString:@"pk"];
//注释的方法是不好的,直接赋值就好
//NSString *str5 = [NSString stringWithString:@"pk"];
NSString *str5 = @"pk";
NSLog(@"str4 = %@,str5=%@",str4,str5);
//3)格式化创建字符串
NSString *str6 = [[NSString alloc] initWithFormat:@"%d_%d_%d_%d_%d_%@",1,2,3,4,5,str4];
//判断字符串是否相等
if([str4 isEqualToString:str5]){
NSLog(@"字符串相等");
}else{
NSLog(@"字符串不相等");
}
NSLog(@"%p,%p",str4,str5);
//判断字符串
if(str4 == str5){
NSLog(@"是同一个对象!");
}
2.基本数据类型和字符串之间的转化
1)字符串和数组
NSString *str = [NSString stringWithFormat:@"%d_%d_%d",1,2,3];
NSArray *arr = [str componentsSeparatedByString:@"_"];
NSLog(@"%@",arr);
/*
2013-11-28 20:31:18.083 根据出生日期计算星座[2013:303] (
1,
2,
3
)
*/
2)字符串截取 和查找子串
NSString *str6 = @"lichanis a ";
//字符串截取
NSLog(@"substring to 2 :%@",[str6 substringToIndex:5]);
NSLog(@"substring to 2 :%@",[str6 substringFromIndex:5]);
//截取某一个范围的字符串
NSRange rang;
rang.length = 4;
rang.location = 2; //截取的时候,包含起始位置
NSLog(@"substring to 2 :%@",[str6 substringWithRange:rang]);
NSString *str8=@"hello01.txt";
//查找 ,返回范围
NSRange rang2 = [str8 rangeOfString:@"llo"];
if(rang2.location != NSNotFound){
NSLog(@"sub str location =%ld ,length=%ld",rang2.location,rang2.length);
}else{
NSLog(@"NSNotFound!!");
}
/*
2013-11-28 20:34:24.767 根据出生日期计算星座[2034:303] sub str location =2 ,length=3
*/
3)可变子串
//可变长度的字符串
NSMutableString *mutableStr = [NSMutableString stringWithString:@"爱大米"];
//动态的插入内容
[mutableStr insertString:@"老鼠" atIndex:0];
NSLog(@"mubableStr:%@",mutableStr);
*/
2013-11-28 20:36:51.071根据出生日期计算星座[2047:303] mubableStr:老鼠爱大米
//--------------------------------NSArray---------------------------
/*
//定义数组并且初始化
NSArray *array1 = [NSArray arrayWithObject:@"one"];
NSArray *array2 = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",nil];
NSArray *array3 = [NSArray arrayWithArray:array2];
NSLog(@"array1 = %@, array2 = %@, array3 = %@",array1,array2,array3);
//数组的访问
//求长度
int len = [array2 count];
//访问元素
NSString *arrayObject = [array3 objectAtIndex:3];
//将数组元素连接成一个字符串
NSString *newStr = [array2 componentsJoinedByString:@"_"];
NSLog(@"array2 length:%d,index 3=%@,joinStr = %@",len,arrayObject,newStr);
//可变数组的使用
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"one", nil];
//----添加元素
[mutableArray addObject:@"two"];
[mutableArray addObject:@"three"];
[mutableArray addObject:@"four"];
//-------------添加一个数组
[mutableArray addObjectsFromArray:array2];
//----计算长度
int length = [mutableArray count];
NSLog(@"mutableArray length=%d,countent:%@",length,mutableArray);
//---- 移除最后一个
[mutableArray removeLastObject];
//---- 移除指定的数据
[mutableArray removeObjectAtIndex:0];
length = [mutableArray count];
NSLog(@"***mutableArray length=%d,countent:%@",length,mutableArray);
//数组的遍历方式:传统方式 高效方式
//----- 传统方式
for (int i=length-1; i>=0; i--) {
NSLog(@"%d = %@",i,[mutableArray objectAtIndex:i]);
}
//----- 高效方式
for(NSString *str in mutableArray){
NSLog(@"obj =%@",str);
}
*/
4.NSDIctionary
NSNumber *numObj = [NSNumber numberWithInt:100];
//初始化一组数组
// 值 key
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:numObj forKey:@"key1"];
//初始化多组数据
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"hello",@"key2",@"world",@"key3",@"csdn",@"key4", nil];
//用一个字典初始化另外一个字典
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
//打印输出
NSLog(@"dic1 : %@,dic2 : %@, dic3 : %@",dic1,dic2,dic3);
2013-11-28 20:38:51.382根据出生日期计算星座[2056:303] dic1 : {
key1 = 100;
},
dic2 : {
key2 = hello;
key3 = world;
key4 = csdn;
},
dic3 : {
key2 = hello;
key3 = world;
key4 = csdn;
}
//获取长度
int len = [dic2 count];
NSLog(@"dic2 length = %d",len);
//根据key获取key所对应的value
NSLog(@"key3 value = %@",[dic2 objectForKey:@"key3"]);
//可以获取所有的keys
NSArray *allkeys = [dic3 allKeys];
NSLog(@"NSarray allkey = %@",allkeys);
//可以获取所有的values
NSArray *allvalues = [dic3 allValues];
NSLog(@"NSarray allvalues = %@",allvalues);
/*
2013-11-28 20:40:54.508 根据出生日期计算星座[2063:303] dic2 length = 3
2013-11-28 20:40:54.509 根据出生日期计算星座[2063:303] key3 value = world
2013-11-28 20:40:54.509 根据出生日期计算星座[2063:303] NSarray allkey = (
key4,
key3,
key2
)
2013-11-28 20:40:54.510 根据出生日期计算星座[2063:303] NSarray allvalues = (
csdn,
world,
hello
)
*/
4.可变NSMutableDIctionary
//----- 可变字典
//----- 初始化
NSMutableDictionary *dic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"key4",@"two",@"key5", nil];
NSLog(@"dic4 : %@",dic4);
//定义成空字典
NSMutableDictionary *dic5 = [NSMutableDictionary dictionary];
//讲字典dic2整体添加到dic4钟
[dic4 addEntriesFromDictionary:dic2];
NSLog(@"addEntriesFromDictionary dic2 : %@",dic4);
//添加一个元素
[dic4 setValue:@"three" forKey:@"key6"];
NSLog(@"dic4 setValue : %@",dic4);
//根据key获取value
NSLog(@"key6 = %@",[dic4 objectForKey:@"key6"]);
//------ 字典的遍历
//1)一般的遍历
NSArray *keys4 = [dic4 allKeys];
for(int i=0;i<[dic4 count];i++){
NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4 objectForKey:[keys4 objectAtIndex:i]]);
}
NSLog(@"-----------------------");
5.for循环
for (NSString *key in dic4){
NSLog(@"dic4 key = %@ ,value = %@",key,[dic4 objectForKey:key]);
}
NSLog(@"-----------------------");
//3)使用枚举进行遍历
NSEnumerator *enum1 = [dic4 keyEnumerator];
//获取key,如果不为空,则进行偏移
id key = [enum1 nextObject];
while (key) {
NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);
key = [enum1 nextObject];
}
6.NSSet 集合
//----------------------------------NSSet-------------------
//----- 定义、初始化
NSSet *set = [[NSSet alloc] initWithObjects:@"one",@"one",@"two",nil];
//用数组定义NSSet;
NSArray *arrayset = [NSArray arrayWithObjects:@"1",@"2",@"3",@"1",nil];
NSSet *set2 = [NSSet setWithArray:arrayset];
NSLog(@"set1 = %@,set2 = %@",set,set2);
//访问方式
//----- 获取长度
int len = [set2 count];
NSString *s = [set2 anyObject];
NSLog(@"set2 length = %d,obj = %@",len,s);
/* //对象不能重复/
2013-11-28 20:46:25.256 根据出生日期计算星座[2079:303] set1 = {(
one,
two
)},set2 = {(
3,
1,
2
)}
2013-11-28 20:46:25.259 根据出生日期计算星座[2079:303] set2 length = 3,obj = 3
*/