Can someone help me to extract int timestamp value from this string "/Date(1242597600000)/" in Objective C
有人可以帮我从Objective C中的字符串“/ Date(1242597600000)/”中提取int时间戳值
I would like to get 1242597600000.
我想得到1242597600000。
Thx
3 个解决方案
#1
One simple method:
一个简单的方法:
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];
Alternatively if you know the string will always be the same length and format, you could use:
或者,如果您知道字符串将始终具有相同的长度和格式,您可以使用:
NSString *numberString = [timestampString substringWithRange:NSMakeRange(7,13)];
And another method:
另一种方法:
NSRange openBracket = [timestampString rangeOfString:@"("];
NSRange closeBracket = [timestampString rangeOfString:@")"];
NSRange numberRange = NSMakeRange(openBracket.location + 1, closeBracket.location - openBracket.location - 1);
NSString *numberString = [timestampString substringWithRange:numberRange];
#2
There's more than one way to do it. Here's a suggestion using an NSScanner;
这样做的方法不止一种。这是一个使用NSScanner的建议;
NSString *dateString = @"\/Date(1242597600000)\/";
NSScanner *dateScanner = [NSScanner scannerWithString:dateString];
NSInteger timestamp;
if (!([dateScanner scanInteger:×tamp])) {
// scanInteger returns NO if the extraction is unsuccessful
NSLog(@"Unable to extract string");
}
// If no error, then timestamp now contains the extracted numbers.
#3
NSCharacterSet* nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSString* digitString = [timestampString stringByTrimmingCharactersInSet:nonDigits];
return [digitString longValue];
#1
One simple method:
一个简单的方法:
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];
Alternatively if you know the string will always be the same length and format, you could use:
或者,如果您知道字符串将始终具有相同的长度和格式,您可以使用:
NSString *numberString = [timestampString substringWithRange:NSMakeRange(7,13)];
And another method:
另一种方法:
NSRange openBracket = [timestampString rangeOfString:@"("];
NSRange closeBracket = [timestampString rangeOfString:@")"];
NSRange numberRange = NSMakeRange(openBracket.location + 1, closeBracket.location - openBracket.location - 1);
NSString *numberString = [timestampString substringWithRange:numberRange];
#2
There's more than one way to do it. Here's a suggestion using an NSScanner;
这样做的方法不止一种。这是一个使用NSScanner的建议;
NSString *dateString = @"\/Date(1242597600000)\/";
NSScanner *dateScanner = [NSScanner scannerWithString:dateString];
NSInteger timestamp;
if (!([dateScanner scanInteger:×tamp])) {
// scanInteger returns NO if the extraction is unsuccessful
NSLog(@"Unable to extract string");
}
// If no error, then timestamp now contains the extracted numbers.
#3
NSCharacterSet* nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSString* digitString = [timestampString stringByTrimmingCharactersInSet:nonDigits];
return [digitString longValue];