标签:
//-------------字符串-----------------
//字符串的初始化:
//1:
NSString *string = @"我是非凡程序员!";
NSLog(@"%@",string);
//2:
NSString *string2 = [NSString stringWithFormat:@"我在非凡学习了%i个月",1];
NSLog(@"%@",string2);
//输出字符串长度:
long length = [string length];
NSLog(@"长度:%lu",length);
//字符串比较: is Equal To
NSString *string3 =@"非凡程序员";
if([string3 isEqualToString:@"非凡员工"])//返回BOOL类型
{
NSLog(@"相等");
}
else
{
NSLog(@"不相等");
}
//字符串的比较 Compare //返回值是 -1,0,1
NSString *str = @"123";
NSString *str1 = @"456";
//str > str1 输出1,str < str1 输出-1,sr = str1 输出0
//字符若是字母,区别大小写,转成ASC码
NSLog(@"%li",[str compare:str1]);
//字母不区分大小写,其余与compare 相同
NSLog(@"%li",[str caseInsensitiveCompare:str1]);
//字符串转化:
NSString *string4 = @"3.141";
float f = [string4 floatValue];
NSLog(@"%f",f);
NSLog(@"%.2f",f);
// float i = [string4 intValue];
// NSLog(@"%i",i);
//字符串的截取;
NSString *string5 =@"abcdefg";
//1.从开始位置截取到下标为3的字符串(不包括3);
NSString *stringnews = [string5 substringToIndex:3];
NSLog(@"%@",stringnews);
//2。从该位置截取到结尾:
stringnews =[string5 substringFromIndex:5];
NSLog(@"%@",stringnews);
//3取一个范围 第一个数字是起始位置,第二个数字是要取的长度:
NSRange range ={1,5};
NSString *result = [string5 substringWithRange:range];
NSLog(@"%@",result );
//字符串扩展:
NSString *string6 =@"abCdEF";
//转大写:
NSString *upper =[string6 uppercaseString];
NSLog(@"%@",upper);
//转小写:
NSString *lower =[string6 lowercaseString];
NSLog(@"%@",lower);
//首字母大写,其余转小些:
NSString *ul =[string6 capitalizedString];
NSLog(@"%@",ul);
//------------------可变字符串---------------:
//1.在OC中,直接赋值为不可变类型,一般可变类型都会需要申请内存空间
//申请一块空间,返回一个空字符串
NSMutableString *echoI = [NSMutableString string];
//2.返回一个空字符串,申请一个初始存储空间,可自动增长。
//NSMutableString *echoIII = [NSMutableString stringWithCapacity:10];
//3.直接初始化
NSMutableString *mutableString = [[NSMutableString alloc] initWithString :@"大家好"];
NSLog(@"%@",mutableString);
//追加字符串 append
[mutableString appendString:@"Hello"];
//删除字符串 delete
//[echoII deleteCharactersInRange: ]
//查找相关的字符串
NSRange rangeI = [mutableString rangeOfString:@"H"];
//location代表找到的开始位置,length代表找到的长度,最小为0
NSLog(@"%lu , %lu", rangeI.length , rangeI.location );
//替换字符串 方法一
NSString *muStr= [mutableString stringByReplacingOccurrencesOfString:@"el" withString:@"111111"];
NSLog(@"%@",muStr);
//替换: 方法二
NSRange range1 = {2,1};
[mutableString replaceCharactersInRange:range1 withString:@"不好"];
NSLog(@"%@",mutableString);
//1.增加
//1-1追加值
[mutableString appendString:@"下午"];
NSLog(@"%@",mutableString);
//1-2
[mutableString insertString:@"上午" atIndex:0];
NSLog(@"%@",mutableString);
//查找
NSRange Range2 =[mutableString rangeOfString:@"上午"];
NSLog(@"%ld",Range2.length);
//删除
[mutableString deleteCharactersInRange:Range2];
NSLog(@"%@",mutableString);
//---------------数组------------------
//数组即相同数据类型的有序集合,数组有下标,下标从0开始
//定义数组:不可变 不可变是不可改变相关数组的序列
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
NSArray *arrayI = @[ @"1", @"2" ];
//输出数组:
for (int i=0;i<4;i++)
{
NSLog(@"%@",array[i]);
}
NSLog(@"%@", arrayI[1] );
//求数组长度:
long len =[array count];
NSLog(@"数组的长度是%lu",len);
//根据下标找元素内容:
NSLog(@"第二个位置是%@",[array objectAtIndex:2]);
// 找一个元素的位置:
long l=[array indexOfObject:@"d"];
NSLog(@"l=%lu",l);
//判断数组是否有某元素:
if([array containsObject:@"e"])
{
NSLog(@"包含");
}
else
{
NSLog(@"不包含");
}
//将数组写入到文件中:
[array writeToFile:@"/Users/a11/Desktop/test.plist" atomically:YES];
//将文件读取到数组:
NSArray *readArr =[NSArray arrayWithContentsOfFile:@"/Users/a11/Desktop/test.plist"];
NSLog(@"%@",readArr);
//将一个数组复制到另一个数组:
NSArray *array2 =[NSArray arrayWithArray:array];
NSLog(@"array=%@",array2);
//数组的比较:
NSArray *array3 =[NSArray arrayWithObjects:@"x",@"y",@"z", nil];
if([array isEqualToArray:array3])
{
NSLog(@"array=array3");
}
else
{
NSLog(@"不相等");
}
//-----------------可变数组----------------
//1.初始化一个可变数组
//1-1可变需要申请一块内存空间
NSMutableArray *muArray = [NSMutableArray array];
//1-2申请初始的内存空间,返回一个空Array
NSMutableArray *muArrayI = [NSMutableArray arrayWithCapacity:10];
//给数组添加对象
//1-1在末尾增加
[muArray addObject:@"a"];
[muArray addObject:@"b"];
NSLog(@"%@",muArray);
//1-2在任意位置插入值:
[muArray insertObject:@"搅局" atIndex:2];
//把不可变的Array变成可变的Array
NSMutableArray *muArrayII= [NSMutableArray arrayWithArray:muArray];
//如果初始长度为2,那追加一个数据到可变数组里此事的长度为3,也就是最大下标就是2
//追加用add
[muArrayII addObject:@"3"];
//得到相关的数组的长度[muArrayII count]
NSLog(@"%@, %li", muArrayII[2], [muArrayII count]);
//删除用remove
[muArrayII removeObject:@"2"];//移除某一个元素
//[muArrayII removeAllObjects];//移除全部
//替换某个下标的数据 为 某个数
[muArrayII replaceObjectAtIndex:1 withObject:@"20"];
//查找某个下标的数据 遍历查找 muArrayII[下标] C的写法
//查找的第一种方法
for( int i = 0 ; i < [muArrayII count]; i++ ){
NSLog(@"for:%@", muArrayII[i] );
}
// NSLog(@"%@, %li", muArrayII[1], [muArrayII count]);
//查找的第二种方法
for(NSString *object in muArrayII){
NSLog(@"%@",object);
}
//----------------字典---------------
//初始化
NSDictionary *dict = @{@"key1":@"value1",@"key2":@"value2"};
NSLog(@"%@",dict);
//NSNumber作为健,字符串作为健,数组作为健,字典作为健
NSDictionary *dict2 = @{@1:@"value1",@"string":@"stringValue",@[@"array",@"array1"]:@"arrayValue",@{@"key1":@"value1"}:@"value"};
NSLog(@"%@",dict2);
//--------------可变字典----------------
NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithCapacity:10];
[muDict setObject:@"杨茹" forKey:@"1-1"];
[muDict setObject:@"down" forKey:@"1-2"];
[muDict setObject:@"left" forKey:@"1-3"];
[muDict setObject:@"right" forKey:@"1-4"];
//1.初始化
//1-1键值通过两个数组设置值。注意:两个数组的个数要相同
NSMutableDictionary *muDictI=[[NSMutableDictionary alloc]initWithObjects:@[@"left",@"right"] forKeys:@[@"1",@"2"]];
//1-2 通过键值同时设置的方式设置,注意这里是双数个数的对象,否则会出现键值不对应,是值-键-值-键的关系。
NSMutableDictionary *muDictII = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
//1-3定义一段内存空间
NSMutableDictionary *muDictIII = [[NSMutableDictionary alloc]initWithCapacity:10];
//不可变字典转换为可变字典
NSMutableDictionary *muDict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
//通过count计算键值对个数
NSLog(@"%li", [muDictI count]);
//根据健取值
id string = [muDict objectForKey:@"1-1"];
NSLog(@"%@",string);
NSLog(@"%@",muDict);
//增加
[muDict setObject:@"forward" forKey:@"1-1"];
NSLog(@"%@",muDict);
//删除
// 1. 删除所有
//[muDict removeAllObjects];
// 2.根据一个键删除对应的键和值
[muDict removeObjectForKey:@"1-1"];
NSLog(@"%@",muDict);
//3.根据多个键删除
[muDict removeObjectsForKeys:@[@"1-1",@"1-2"]];
//注意:一个键对应一个值,因此,通过某个键改变或增加对应的值的时候已经将之前的值覆盖掉
//-------------NSNumber----------
//NSNumber 类型可以是Id、int、float、double、char
NSNumber *number = [[NSNumber alloc]initWithInt:2];
NSLog(@"%@",number);
NSNumber *number2 = @4;
NSLog(@"%@",number2);
//---------------NSSet------------
/*
NSSet与NSArray对比:
NSSet中的元素可以重复,NSSArray不能重复
NSSet是有序的,NSSArray是无序的。
*/
NSSet *set = [NSSet setWithObject:@"20"];
NSLog(@"%@",set);
标签:
原文地址:http://my.oschina.net/u/2483162/blog/517236