ios转换字符串" 5小时10分钟"到整数小时和分钟

时间:2015-04-27 11:14:43

标签: ios objective-c nsstring

我正在尝试从字符串" 5小时10分钟" 获取整数变量中的小时和分钟。可以任何人为我提供解决方案。

.volt

5 个答案:

答案 0 :(得分:7)

请尝试使用以下代码来检索int值。

@try {
    NSString *myString = @"5 hours 10 min";
    NSArray *myWords = [myString componentsSeparatedByCharactersInSet:
                        [NSCharacterSet characterSetWithCharactersInString:@" "]
                        ];
    int hoursValue = 0, minuteValue = 0;
    hoursValue = [[myWords objectAtIndex:0] intValue];
    if([myWords count] > 2) {
        minuteValue = [[myWords objectAtIndex:2] intValue];
    }

    NSLog(@"hours %d and Minute %d",hoursValue,minuteValue);
 }
 @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
 }

答案 1 :(得分:0)

试试这个

    @try
    {
           NSString *myString = @"5 hours 10 min";
           NSArray *myWords = [myString componentsSeparatedByString:@" "];
           int hoursValue = 0, minuteValue = 0;
           if ([myWords count] == 4)
           {
                hoursValue = [[myWords objectAtIndex:0] intValue];
                minuteValue = [[myWords objectAtIndex:2] intValue];

           }
           else if([myWords count] == 2)
           {
                if([[myWords objectAtIndex:1] isEqualToString:@"hours"] == yes)
                {
                    hoursValue = [[myWords objectAtIndex:0] intValue];
                }
                else
                {
                    minuteValue = [[myWords objectAtIndex:0] intValue];
                }
           }   
           NSLog(@"hours %d and Minute %d",hoursValue,minuteValue);
    }
    @catch(Exception *exception
    {
    }

答案 2 :(得分:0)

使用NSDateFormatter将string1转换为NSDate,然后获取所需的NSDateComponents:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"<Date format>"];
    NSDate *date = [dateFormatter dateFromString:string1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];

了解详情here

答案 3 :(得分:0)

试试这个:

int hr,mins;
NSString *str=@"5 hours 10 min";
NSArray *firstWords = [str componentsSeparatedByString:@" "];
if (([[firstWords objectAtIndex:1] isEqualToString:@"hours"]||[[firstWords objectAtIndex:1] isEqualToString:@"hr"] ) &&([[firstWords objectAtIndex:3] isEqualToString:@"minutes"]||[[firstWords objectAtIndex:3] isEqualToString:@"min"])) {
    hr=(int)[[firstWords objectAtIndex:0]integerValue];
    mins=(int)[[firstWords objectAtIndex:2]integerValue];
    if (mins > 60) {
        hr+=mins/60;
        mins    =mins%60;

    }
}

答案 4 :(得分:-2)

试试这个

- (NSString *)timeFormatted:(int)totalSeconds
{

    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds / 60) % 60; 
    int hours = totalSeconds / 3600; 

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; 
}