如何从字符串中提取特定数字?

时间:2014-08-18 07:00:34

标签: objective-c string extract

可能已经谈了很多。不要生气。但我不能按照我的意愿使这个字符串结束。我使用NSMakeRange进行切割,但这没有用,因为天数变化,我的射程也不同。

我需要提取:02 35这样的字符串&#34; [fire date = lunes,18 de agosto de 2014 02h&#39; 35:28 Horas stander de Argentina ...&#34; < / p>

编辑:

好的,没有......

我需要提取:02 35这样的字符串&#34; < UIConcreteLocalNotificacion: 0x8e874f0> [fire date = lunes,18 de agosto de 2014 02h&#39; 35:28 Horas estandar de Argentina&#34; < / p>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *localNotification = [localNotifications objectAtIndex:indexPath.row];

[cell.textLabel setText:localNotification.alertBody];

NSString *strdelArray = [[localNotifications valueForKey:@"description"] componentsJoinedByString:@""];
NSString *strdelArrayCortado = [strdelArray substringWithRange: NSMakeRange(53, 37)];

[cell.detailTextLabel setText: strdelArray];
labelParse.text = strdelArrayCortado;

// this show "lunes, 18 de agosto de 2014 02h'35:28"
//try pass string to numbers.. but only see 18

NSString *numberString;

NSScanner *scanner = [NSScanner scannerWithString:strdelArrayCortado];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];


[scanner scanUpToCharactersFromSet:numbers intoString:NULL];

[scanner scanCharactersFromSet:numbers intoString:&numberString];

labelParse.text = numberString;
return cell;}"

1 个答案:

答案 0 :(得分:1)

首先将您的字符串转换为日期格式: -

    fireDateStr = @"lunes, 18 de agosto de 2014 02h'35:28 Horas";  
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss ZZZ";
    NSDate *dateAndTime = [formatter dateFromString: fireDateStr];

然后像这样抽出你的时间: -

 NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"HH:mm"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];// Set timeZone according to your requirement.

    NSString *newFireDateStr =[dateFormatter1 stringFromDate:dateAndTime];