1.创建NSString字符串 NSString 与 char* 最大的差别就是 NSString是一个objective对象,而char* 是一个字节数组。 @+" 字符串 " 这个符号为objective-c NSString 字符串常量的标准使用方法。char* 创建的时候 无需加入@ - (void)viewDidLoad [super viewDidLoad]; //经典的字符串赋值 NSString *str0 = @"my name is justcoding !"; //字符串格式化合并分别包含 //NSString*类型 int类型 char*类型 NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"[email protected]"]; //字符串赋值 參数中仅仅能够写一个字符串 和第一种非常像 NSString *str2 = [NSString stringWithString:@"我是字符串"]; //字符串转换为utf-8格式 參数为char*类型 NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"]; //字符串合并 int i = 100; char*c = "xuanyusong"; NSString *temp = @"我是暂时字符串"; //在字符串temp的基础继续加入 int i 与 char* c 组成一个新的字符串 NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c]; //在字符串temp的基础继续加入temp 并组成一个新的字符串 NSString *str5 = [temp stringByAppendingString:temp]; //字符串输出 NSLog(@"str0 = %@", str0); NSLog(@"str1 = %@", str1); NSLog(@"str2 = %@", str2); NSLog(@"str3 = %@", str3); NSLog(@"str4 = %@", str4); NSLog(@"str5 = %@", str5); - (void)viewDidLoad { [super viewDidLoad]; //经典的字符串赋值 NSString *str0 = @"my name is justcoding !"; //字符串格式化合并分别包含 //NSString*类型 int类型 char*类型 NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"[email protected]"]; //字符串赋值 參数中仅仅能够写一个字符串 和第一种非常像 NSString *str2 = [NSString stringWithString:@"我是字符串"]; //字符串转换为utf-8格式 參数为char*类型 NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"]; //字符串合并 int i = 100; char*c = "xuanyusong"; NSString *temp = @"我是暂时字符串"; //在字符串temp的基础继续加入 int i 与 char* c 组成一个新的字符串 NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c]; //在字符串temp的基础继续加入temp 并组成一个新的字符串 NSString *str5 = [temp stringByAppendingString:temp]; //字符串输出 NSLog(@"str0 = %@", str0); NSLog(@"str1 = %@", str1); NSLog(@"str2 = %@", str2); NSLog(@"str3 = %@", str3); NSLog(@"str4 = %@", str4); NSLog(@"str5 = %@", str5); } 2.字符串的遍历 每个字符串事实上是由若干个char字符组成。字符串的遍历实际上就是将字符串中的每个字符提取出来。 - (void)viewDidLoad [super viewDidLoad]; //经典的字符串赋值 NSString *str = @"YUSONGMOMO"; //字符串的长度 int count = [str length]; NSLog(@"字符串的长度是%d",count); //遍历字符串中的每个字符 for(int i =0; i < count; i++) char c = [str characterAtIndex:i]; NSLog(@"字符串第 %d 位为 %c",i,c); - (void)viewDidLoad { [super viewDidLoad]; //经典的字符串赋值 NSString *str = @"YUSONGMOMO"; //字符串的长度 int count = [str length]; NSLog(@"字符串的长度是%d",count); //遍历字符串中的每个字符 for(int i =0; i < count; i++) { char c = [str characterAtIndex:i]; NSLog(@"字符串第 %d 位为 %c",i,c); } } 3.字符串的比較 isEqualToString 比較字符串是否全然相等。大写和小写不一样也无法全然匹配。 hasPrefixe 匹配字符串头 haSuffix 匹配字符串的尾巴 - (void)viewDidLoad [super viewDidLoad]; NSString *str0 = @"justcoding"; NSString *str1 = @"justcoding"; //字符串全然相等比較 if([str0 isEqualToString:str1]) NSLog(@"字符串全然相等"); //字符串以开头比較 if([str0 hasPrefix:@"just"]) NSLog(@"字符串str0以just开头"); //字符串以结尾比較 if([str1 hasSuffix:@"coding"]) NSLog(@"str1字符串以coding结尾"); - (void)viewDidLoad { [super viewDidLoad]; NSString *str0 = @"justcoding"; NSString *str1 = @"justcoding"; //字符串全然相等比較 if([str0 isEqualToString:str1]) { NSLog(@"字符串全然相等"); } //字符串以开头比較 if([str0 hasPrefix:@"just"]) { NSLog(@"字符串str0以just开头"); } //字符串以结尾比較 if([str1 hasSuffix:@"coding"]) { NSLog(@"str1字符串以coding结尾"); } } //isEqualToString方法 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result); //isEqualToString方法 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result); //compare方法(comparer返回的三种值) NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedSame 推断两者内容是否同样 //compare方法(comparer返回的三种值) NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedSame 推断两者内容是否同样 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result); //NSOrderedAscending 推断两对象值的大小(按字母顺序进行比較,astring02大于astring01为真) NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result); //NSOrderedAscending 推断两对象值的大小(按字母顺序进行比較。astring02大于astring01为真) NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result); //NSOrderedDescending 推断两对象值的大小(按字母顺序进行比較,astring02小于astring01为真) NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result); //NSOrderedDescending 推断两对象值的大小(按字母顺序进行比較,astring02小于astring01为真) //不考虑大 小写比較字符串1 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedDescending推断两对象值的大小(按字母顺序进行比較。astring02小于astring01为 真) //不考虑大 小写比較字符串1 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedDescending推断两对象值的大小(按字母顺序进行比較,astring02小于astring01为 真) //不考虑大写和小写比較字符串2 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; NSLog(@"result:%d",result); //NSCaseInsensitiveSearch:不区分大写和小写比較 NSLiteralSearch:进行全然比較。区分大写和小写 NSNumericSearch:比較字符串的字符个数,而不是字符值。 //不考虑大写和小写比較字符串2 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; NSLog(@"result:%d",result); //NSCaseInsensitiveSearch:不区分大写和小写比較 NSLiteralSearch:进行全然比較,区分大写和小写 NSNumericSearch:比較字符串的字符个数,而不是字符值。 推断的方法能够利用 NSString 类别中 caseInsensitiveCompare: 所回传 -1、0 或是 1 的数值,判定两个字串之间得排序关系。其程式玛例如以下。 NSString *string = @"0"; NSComparisonResult result = [string caseInsensitiveCompare:@"A"]; switch (result) { case NSOrderedAscending: NSLog(@"升幂"); break; case NSOrderedSame: NSLog(@"忽略大写和小写同样的字串"); break; case NSOrderedDescending: NSLog(@"降幂"); break; default: NSLog(@"无法判定"); break; NSString *string = @"0"; NSComparisonResult result = [string caseInsensitiveCompare:@"A"]; switch (result) { case NSOrderedAscending: NSLog(@"升幂"); break; case NSOrderedSame: NSLog(@"忽略大写和小写同样的字串"); break; case NSOrderedDescending: NSLog(@"降幂"); break; default: NSLog(@"无法判定"); break; } 4. 文件字符串操作(读写) //从文件读取字符串:initWithContentsOfFile方法 NSString *path = @"astring.text"; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring); [astring release]; //astring=nil; //从文件读取字符串:initWithContentsOfFile方法 NSString *path = @"astring.text"; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring); [astring release]; //astring=nil; //写字符串到文件:writeToFile方法 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release]; //astring=nil; //写字符串到文件:writeToFile方法 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release]; //astring=nil; * ios5 不支持release iOS应用开发:什么是ARC? //扩展路径 NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); //扩展路径 NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); //文件扩展名 NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]); //文件扩展名 NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]); 5. 字符串的截取和大写和小写 //改变字符串的大写和小写 NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 //改变字符串的大写和小写 NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 Java代码 - (void)viewDidLoad [super viewDidLoad]; NSString *str0 = @"中文my name is xuanyusong"; //截取字符串起始点到index为4之间的内容 NSString * to = [str0 substringToIndex:4]; NSLog(@"to = %@",to); //截取字符index为2 到结尾之间的内容 NSString * from = [str0 substringFromIndex:2]; NSLog(@"from = %@",from); //设置截取字符串的范围 //从第二位開始,长度为十 NSRange rang = NSMakeRange(2, 10); NSString * strRang = [str0 substringWithRange:rang]; NSLog(@"rang = %@",strRang); //设置字符串首字母大写 NSLog(@"str0首字母大写:%@",[str0 capitalizedString]); //设置字符串所有内容为大写 NSLog(@"str0大写:%@",[str0 uppercaseString]); //设置字符所有内容为小写 NSLog(@"str0小写:%@",[str0 lowercaseString]); - (void)viewDidLoad { [super viewDidLoad]; NSString *str0 = @"中文my name is xuanyusong"; //截取字符串起始点到index为4之间的内容 NSString * to = [str0 substringToIndex:4]; NSLog(@"to = %@",to); //截取字符index为2 到结尾之间的内容 NSString * from = [str0 substringFromIndex:2]; NSLog(@"from = %@",from); //设置截取字符串的范围 //从第二位開始,长度为十 NSRange rang = NSMakeRange(2, 10); NSString * strRang = [str0 substringWithRange:rang]; NSLog(@"rang = %@",strRang); //设置字符串首字母大写 NSLog(@"str0首字母大写:%@",[str0 capitalizedString]); //设置字符串所有内容为大写 NSLog(@"str0大写:%@",[str0 uppercaseString]); //设置字符所有内容为小写 NSLog(@"str0小写:%@",[str0 lowercaseString]); } 6.搜索字符串与替换字符串 - (void)viewDidLoad [super viewDidLoad]; NSString *str0 = @"中文my name is xuanyusong"; NSString *temp = @"is"; NSRange rang = [str0 rangeOfString:temp]; NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location); NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length); //将搜索中的字符串替换成为一个新的字符串 NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"]; NSLog(@"替换后字符串为%@", str); //将字符串中" " 所有替换成 * str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"]; NSLog(@"替换后字符串为%@", str); - (void)viewDidLoad { [super viewDidLoad]; NSString *str0 = @"中文my name is xuanyusong"; NSString *temp = @"is"; NSRange rang = [str0 rangeOfString:temp]; NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location); NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length); //将搜索中的字符串替换成为一个新的字符串 NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"]; NSLog(@"替换后字符串为%@", str); //将字符串中" " 所有替换成 * str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"]; NSLog(@"替换后字符串为%@", str); } 拓展:使用以下这种方法总体替换字符串还能够设置替换的区域。 stringByReplacingOccurrencesOfString:(NSString *)withString:(NSString *) options:(NSStringCompareOptions) range:(NSRange) 7.字符串尾部加入 使用alloc在内存中创建字符串对象后边能够动态的操作这个字符串,改动与加入等。 appendstring 方法:向字符串尾部加入一个字符串。 appendFormat方法:向字符串尾部加入多个类型的字符串。能够加入随意数量与类型的字符串。 - (void)viewDidLoad [super viewDidLoad]; NSMutableString * str = [[NSMutableString alloc] init]; //加入普通的字符串 [str appendString:@"aaa"]; //加入字符串 整型 字符型 [str appendFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"[email protected]"]; NSLog(@"str = %@",str); - (void)viewDidLoad { [super viewDidLoad]; NSMutableString * str = [[NSMutableString alloc] init]; //加入普通的字符串 [str appendString:@"aaa"]; //加入字符串 整型 字符型 [str appendFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"[email protected]"]; NSLog(@"str = %@",str); } 8. 字符串中删除元素 stringWithString 方法:用于创建字符串初始化赋值 rangeOfString方法:传入字符串返回一个在该字符串中的范围 也能够写 NSMakeRange(0, 3) 意思是范围在字符串0位到第3位 deleteCharactersInRange:删除字符串 參数为Range 就是删除的范围。 - (void)viewDidLoad [super viewDidLoad]; //创建字符串 NSMutableString *str = [NSMutableString stringWithString:@"justcoding is best!"]; //删除字符串中含"justcoding"的字符 [str deleteCharactersInRange: [str rangeOfString: @"justcoding"]]; NSLog(@"str = %@",str); - (void)viewDidLoad { [super viewDidLoad]; //创建字符串 NSMutableString *str = [NSMutableString stringWithString:@"justcoding is best!"]; //删除字符串中含"justcoding"的字符 [str deleteCharactersInRange: [str rangeOfString: @"justcoding"]]; NSLog(@"str = %@",str); } 9. 字符串插入 stringWithString 方法:用于创建字符串初始化赋值 insertString方法:第一个參数 插入的字符串对象, 第二个參数 插入的位置。 - (void)viewDidLoad [super viewDidLoad]; //创建字符串 NSMutableString *str = [NSMutableString stringWithString:@"justcoding is"]; //在str第10位插入字符串 [str insertString:@"best man" atIndex:10]; NSLog(@"str = %@",str); - (void)viewDidLoad { [super viewDidLoad]; //创建字符串 NSMutableString *str = [NSMutableString stringWithString:@"justcoding is"]; //在str第10位插入字符串 [str insertString:@"best man" atIndex:10]; NSLog(@"str = %@",str); } 10. 字符串拷贝 - (void)viewDidLoad [super viewDidLoad]; //创建字符串 NSMutableString *str1 = [NSMutableString stringWithString: @"字符串1"]; NSMutableString *str2; //字符串赋值 str2 = str1; [str2 appendString: @" 和字符串2"]; NSLog (@"str1 = %@", str1); NSLog (@"str2 = %@", str2); - (void)viewDidLoad { [super viewDidLoad]; //创建字符串 NSMutableString *str1 = [NSMutableString stringWithString: @"字符串1"]; NSMutableString *str2; //字符串赋值 str2 = str1; [str2 appendString: @" 和字符串2"]; NSLog (@"str1 = %@", str1); NSLog (@"str2 = %@", str2); } 为什么给str2加入数据后 str1的数据也改变了?这就是指针的魅力所在,由于我们操作的是指针,str2 = str1 意思是两个指针同一时候指向一块内存,那么str2指向的内存内容改变以后str1当然跟着改变了。 11. 字符串与指定类型转换 假设转换的參数非法的话不会抛出异常,好比用中文去转整型。 不会报错 可是转换结果为 0 。默认值。 - (void)viewDidLoad [super viewDidLoad]; //字符串转整型 NSString *str0 = @"1121"; //NSString *str0 = @"中国"; //把字符串强转成整型 int i = [str0 intValue]; NSLog (@"转换后:%i", i); //字符串转interger NSString *str1 = @"1985"; // NSString *str1 = @"中国"; //把字符串强转成interger NSInteger ii = [str1 integerValue]; NSLog (@"转换后:%i", ii); //字符串转double NSString *str2 = @"3.145926"; //NSString *str2 = @"中国"; //把字符串强转成double double d = [str2 doubleValue]; NSLog (@"转换后:%f", d); //字符串转float NSString *str3 = @"3.145926"; //NSString *str3 = @"中国"; //把字符串强转成float double f = [str3 floatValue]; NSLog (@"转换后:%f", f); - (void)viewDidLoad { [super viewDidLoad]; //字符串转整型 NSString *str0 = @"1121"; //NSString *str0 = @"中国"; //把字符串强转成整型 int i = [str0 intValue]; NSLog (@"转换后:%i", i); //字符串转interger NSString *str1 = @"1985"; // NSString *str1 = @"中国"; //把字符串强转成interger NSInteger ii = [str1 integerValue]; NSLog (@"转换后:%i", ii); //字符串转double NSString *str2 = @"3.145926"; //NSString *str2 = @"中国"; //把字符串强转成double double d = [str2 doubleValue]; NSLog (@"转换后:%f", d); //字符串转float NSString *str3 = @"3.145926"; //NSString *str3 = @"中国"; //把字符串强转成float double f = [str3 floatValue]; NSLog (@"转换后:%f", f); }
时间: 2024-10-31 10:42:44