//**************************************************************
// NSString
//**************************************************************
//oc所有的内存存储区间数据区,堆区,栈区,代码区
//----------------创建字符串的方法----------------
//1.字符串常量创建
NSString *string_1 = @"wellcome to it";
//2、通过实例化方法 initWithString:实例化一个字符串对象
NSString *string_2 = [[NSString alloc] initWithString:string_1];
//NSString *string_3 = [[NSString alloc] initWithString:@"i love it"];//多余
NSLog(@"%@",string_2);
//3、用标准c创建字符串: initWithCString:encoding:方法
char *str = "i love iOS" ;
NSString *string_4 = [[NSString alloc]initWithCString:str encoding:NSUTF8StringEncoding];
NSLog(@"%@",string_4);
//或者:用 initWithUTF8String:实例化一个字符串对象
NSString *string_5 = [[NSString alloc]initWithUTF8String:str];
NSLog(@"%@",string_5);
//4、创建格式化字符串:占位符(由一个%加一个字符组成)
int age = 23;
NSString *name = @"陈梁";
NSString *string_6 = [[NSString alloc]initWithFormat:@"i an %@,and age is %i 。" ,name ,age ];
NSLog(@"%@",string_6);
//5、通过静态方法创建字符串对象
NSString *string_7 = [[NSString alloc] initWithString:@"帅哥"];
NSLog(@"%@",string_7);
NSString *string_8 = [[NSString alloc]initWithUTF8String:str];
NSLog(@"%@",string_8);
NSString *string_9 = [[NSString alloc] initWithFormat:@"i love %@",@"iOS" ];
NSLog(@"%@",string_9);
//6. 从文件读取字符串:initWithContentsOfFile:
NSString *path = @"/Users/student/Desktop/test.m"; //文件路径
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
// C字符串 --> OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
// OC字符串 --> C字符串
const char *cs = [s4 UTF8String];
//----------------比较两个字符串----------------
//c语言中比较两个字符串
char string[] = "iOS";
char string1[] = "安卓";
if (strcmp(string,string1)>0) {
NSLog(@"1");
}
//1. isEqualToString方法 判断两个字符串是否相等 相等返回 1 不等返回 0
BOOL result = [ string_9 isEqualToString: @"i love iOS"];
NSLog(@"%i",result);
//2. compare 方法(compare:返回的三种值 分别是-1,0,1)
BOOL result_1 = [string_5 compare:string_9];//相同返回0 ,不同返回 1
NSLog(@"%i",result_1);
//NSOrderedSame 判断两者内容是否相同
BOOL result_2 = [string_5 compare:string_9] == NSOrderedSame;//相同为1,不同为0
NSLog(@"%i",result_2);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,result_1小于result_2 为真)
BOOL result_3 = [@"ab" compare:@"b"] ==NSOrderedAscending ;
NSLog(@"%i",result_3);//1小于2返回1 ,
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,result_1大于result_2为真)
BOOL result_4 = [@"ab" compare:@"b"] ==NSOrderedDescending ;
NSLog(@"%i",result_4);//1大于2返回1
//3. 不考虑大小写比较字符串
//与不区分大小写仅仅是方法名不同
BOOL result_5 = [@"ab" caseInsensitiveCompare:@"b"] ==NSOrderedAscending ;
NSLog(@"%i",result_5);//1小于2返回1
//----------------改变字符串的大小写----------------
//1. uppercaseString 小写转大写 (原字符串内容不改变)
NSString *string_10 = [string_9 uppercaseString];
NSLog(@"%@",string_10);
//2. lowercaseString 大写转小写
NSString *string_11 = [string_10 lowercaseString];
NSLog(@"%@",string_11);
//3. capitalizedString (单词 首字母大写 其余小写,其他字符保持不变
NSLog(@"%@",[string_10 capitalizedString]);
//----------------搜索子串----------------
NSString *string3 = @"i love iOS";
NSString *string2 = @" love ";
NSRange range = [string3 rangeOfString:string2];
NSLog(@"length = %ld,location = %ld",range.length,range.location);
//----------------抽取子串 --substring--------------
//1. -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string4 = [string3 substringToIndex:6];
NSLog(@"%@",string4);
//2. -substringFromIndex: 从指定位置开始(包括指定位置的字符)提取,并包 括之后的全部字符 直到最后;
NSLog(@"%@",[string3 substringFromIndex:6]);
//3. -substringWithRange: //在指定范围内从字符串中截取子串
NSLog(@"%@",[string3 substringWithRange:NSMakeRange(7, 7)]);
//------------判断字符串内是否还包含别的字符串(前缀,后缀)--has----------
//01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *)aString; (判断前缀)
NSLog(@"%i",[string3 hasPrefix:@"i love"]);
//02:检查字符串是否以另一个字符串结尾- (BOOL) hasSuffix: (NSString *)aString; (判断后缀)
NSLog(@"%i",[string3 hasSuffix:@"iOS"]);
//**************************************************
// 二.NSMutableString
// **********************************************
//---------------给字符串分配预设空间大小----------------
//stringWithCapacity:预设空间
NSString *string5 = [NSMutableString stringWithCapacity:20];
string5 = @"i love computer and my wife " ;
//---------------增加字符串----------------
//1. appendString: appendFormat: 追加,在后面追加字符串
NSMutableString *string6 = [[NSMutableString alloc] initWithString:string5];
[string6 appendString:string2];
[string6 appendFormat:@"iOS"];
NSLog(@"%@",string6);
//2. -insertString: atIndex: 在指定位置插入字符串
[string6 insertString:string2 atIndex:19];
NSLog(@"%@",string6);
//--------在已有字符串中按照所给出范围和长度删除字符------
//deleteCharactersInRange: 在指定范围内删除字符串
[string6 deleteCharactersInRange:NSMakeRange(0, 6)];
NSLog(@"%@",string6);
//--------修改字符串------
//-setString: 修改字符串
[string6 setString:string3];
NSLog(@"%@",string6);
//--------在指定范围内,替换的原有的字符------
// replaceCharactersInRange: withString: 替换
[string6 replaceCharactersInRange:NSMakeRange(0,1) withString:@"iOS is my love"];
NSLog(@"%@",string6);
NSRange range4 = [@"mrchen" rangeOfString:@"chen"];
NSLog(@"%lu--%li",range4.length,range4.location);
NSString *s = [@"mr" stringByAppendingString:@"chen"];
NSLog(@"%@",s);
//字符串导出
[@"chen jingbaoying" writeToFile:@"/Users/student/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:Nil];//会替换原来的my.txt
NSURL *url = [NSURL fileURLWithPath:@"/Users/student/Desktop/my.txt"];
[@"iOS love" writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];