一、字符串的操作方法
#pragma mark - 创建字符串 - (void)CreatString { // <1> 创建字符创 NSString *strS = @"This is a String!"; // <2> 创建空字符串,给予赋值 NSString *strK = [[NSString alloc] init]; strK = @"This is a String!"; NSLog(@"strS:%@, strK%@",strS,strK); // <3> 使用变量初始化字符串 NSString *name = @"aofe"; NSString *strN = [NSString stringWithFormat:@"My name is %@",name]; NSLog(@"strN:%@",strN); } #pragma mark - 判断是否包含某字符串 - (void)IsContainsString { NSString *str1 = @"NSStringInformation.txt"; // <1> 检查字符串是否以另一个字符串开头 [str1 hasPrefix:@"NSString"] == 1? NSLog(@"YES") :NSLog(@"NO"); // <2> 检查字符串是否以另一个字符串结尾 [str1 hasSuffix:@".txt"] == 1? NSLog(@"YES") :NSLog(@"NO"); // <3> 检查字符串是否包含其他字符串 [str1 rangeOfString:@"Information"].length > 0 ? NSLog(@"YES") :NSLog(@"NO"); // <4> 检查字符串是否包含其他字符串 [str1 containsString:@"abc"]?NSLog(@"YES"):NSLog(@"NO"); } #pragma mark - 读写字符串 -(void)WriteAndReadString { // 文件路径 NSString *path = @"wenJianQuanLuJing"; // <1> 从文件读取字符串 NSString *strW = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; // <2> 将字符串写入到文件 [strW writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL]; } #pragma mark - 比较两个字符串 -(void)ComparerTwoString { NSString *str1 = @"This is String1"; NSString *str2 = @"THIS is String2"; // 比较两个字符串是否相等 BOOL result1 = [str1 isEqualToString:str2]; // 比较两个字符串(comparer方法返回三种值:NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending) NSComparisonResult result2 = [str1 compare:str2]; // 不考虑大小比较字符串 NSComparisonResult result3 = [str1 caseInsensitiveCompare:str2]; NSLog(@"result1:%d,result2:%ld,result3:%ld",result1,(long)result2,(long)result3); } #pragma mark - 改变字符串的大小写 -(void)ChangeStringCase { NSString *str1 = @"this is string1"; NSString *str2 = @"THIS IS STRING2"; // 全部大写 [str1 uppercaseString]; // 全部小写 [str2 lowercaseString]; // 首字母大写 [str1 capitalizedString]; } #pragma mark - 在字符串中搜索子串 -(void)SearchString { NSString *str1 = @"This is String1"; NSString *str2 = @"is"; NSRange range = [str1 rangeOfString:str2]; NSLog(@"location:%lu,length:%lu",(unsigned long)range.location,(unsigned long) range.length); } #pragma mark - 替换字符串 -(void)ReplaceString { NSString *strL = @"hello china"; NSString *strN = [strL stringByReplacingOccurrencesOfString:@"china" withString:@"beijing"]; NSLog(@"strL:%@,strN:%@",strL,strN); } #pragma mark - 分隔字符串成数组 -(void)ComponentsString { NSString *str = @"a b c d e"; // 以空格分隔字符串成数组 NSArray *arr = [str componentsSeparatedByString:@" "]; NSLog(@"arr:%@,arr.count:%lu",arr,(unsigned long)arr.count); } #pragma mark - 数组拼接成字符串 -(void)ArrayToString { NSArray *array = [NSArray arrayWithObjects:@"this",@"is",@"String", nil]; // 用空格隔开数组中的元素 NSString *str = [array componentsJoinedByString:@" "]; NSLog(@"str:%@",str); } #pragma mark - 从字符串中抽取出新的字符串 -(void)DrawNewString { NSString *str = @"This is String"; // 从字符串的开头一直截取到指定位置,但不包括该位置的字符. NSString *str1 = [str substringToIndex:5]; // 从指定位置开始(包括自定位置的字符串)一直到最后 NSString *str2 = [str substringFromIndex:5]; // 按照所给定的位置和长度,任意的从字符串中截取新的字符串 NSString *str3 = [str substringWithRange:NSMakeRange(4, 5)]; NSLog(@"str1:%@,str2:%@,str3:%@",str1,str2,str3); }
二、可变字符串
- (void)NSMutableStringOperation { // 给字符串分配容量 NSMutableString *strM = [NSMutableString stringWithCapacity:100]; NSLog(@"strM:%@",strM); // 在已有的字符串后面添加字符串 NSMutableString *strM1 = [[NSMutableString alloc] initWithString:@"This is a"]; NSLog(@"strM1:%@",strM1); [strM1 appendString:@"NSMutableString"]; NSLog(@"strM1:%@",strM1); // 在已有字符串中按照所给出的范围和长度删除字符 [strM1 deleteCharactersInRange:NSMakeRange(0, 5)]; NSLog(@"strM1:%@",strM1); // 在字符串指定位置插入字符串 [strM1 insertString:@"Hello" atIndex:0]; NSLog(@"strM1:%@",strM1); // 将已有字符串替换成其他字符串 [strM1 setString:@"Hello World"]; NSLog(@"strM1:%@",strM1); // 按照所给出的范围,用新字符串替换原来的字符串 [strM1 replaceCharactersInRange:NSMakeRange(0, 5) withString:@"Hi"]; NSLog(@"strM1:%@",strM1); }
三、时间操作
- (void)NSDateOperation { // 得到当前日期 NSDate *date1 = [NSDate date]; NSLog(@"date:%@",date1); NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:3]; // 得到当前时间:coreFoundation框架方法 double currentTime =CFAbsoluteTimeGetCurrent(); // 比较日期: // <1> 比较日期是否相同 BOOL result =[date1 isEqualToDate:date2]; // <2> 比较日期 返回时间较早的日期 NSDate *dateE = [date1 earlierDate:date2]; // <3> 比较日期 返回时间较晚的日期 NSDate *dateL = [date1 laterDate:date2]; NSLog(@"result:%d, dateE:%@, dateL:%@",result,dateE,dateL); // 设置日期的格式 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 设置几种默认的显示效果 [formatter setTimeStyle:NSDateFormatterMediumStyle]; //自定义日期显示效果,H大写为24小时计时 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *time = [formatter stringFromDate:date1]; NSLog(@"%@",time); }
时间: 2024-10-29 19:12:13