In my app I have a string - "contest will start from dd/mm/yyyy to dd/mm/yyyy" I want both of the dates separately so I need a way to store these dates in two different strings.
user1754580
问问题
195 次
3 回答
1
您可以使用正则表达式来做到这一点(半明目张胆地改编自这里)。
NSString *string = @"contest will start from 10/11/2012 to 12/11/2012";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"from (.*) to (.*)"
options:0
error:NULL];
NSTextCheckingResult *results = [regex firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])];
NSString *date1 = [string substringWithRange:[results rangeAtIndex:1]];
NSString *date2 = [string substringWithRange:[results rangeAtIndex:2]];
于 2012-11-11T00:30:39.167 回答
0
NSString *dateString = @"contest will start from dd/mm/yyyy to dd/mm/yyyy";
NSArray *components = [dateString componentsSeparatedByString:@"to"];
NSString *firstDate = [[components objectAtIndex:0] stringByReplacingOccurrencesOfString:@"contest will start from "];
NSString *secondDate = [[components objectAtIndex:1] stringByReplacingOccurrencesOfString:@" "];
于 2012-11-11T00:16:50.677 回答
0
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:units fromDate: date];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSString *together = [NSString stringWithFormat:@"%i - %i - %i", day, month, year];
于 2012-11-11T00:35:46.777 回答