有人可以帮我从 objective-c 中的此字符串“/Date(1242597600000)/”中提取int时间戳值吗?

我想获得1242597600000。

谢谢

最佳答案

一种简单的方法:

NSString *timestampString = @"\/Date(1242597600000)\/";
NSArray *components = [timestampString componentsSeparatedByString:@"("];
NSString *afterOpenBracket = [components objectAtIndex:1];
components = [afterOpenBracket componentsSeparatedByString:@")"];
NSString *numberString = [components objectAtIndex:0];
long timeStamp = [numberString longValue];

或者,如果您知道字符串的长度和格式始终相同,则可以使用:
NSString *numberString = [timestampString substringWithRange:NSMakeRange(7,13)];

还有另一种方法:
NSRange openBracket = [timestampString rangeOfString:@"("];
NSRange closeBracket = [timestampString rangeOfString:@")"];
NSRange numberRange = NSMakeRange(openBracket.location + 1, closeBracket.location - openBracket.location - 1);
NSString *numberString = [timestampString substringWithRange:numberRange];

10-06 04:54