【发布时间】:2015-05-08 19:09:46
【问题描述】:
我有一个这样的 NSString。
00:03:45
我只想得到这个 03:45 部分并显示在 UILabel 中
我该怎么做?请帮帮我
谢谢
【问题讨论】:
-
你可以从实际阅读 NSString 的文档开始。
-
同意。 Apple 的文档足以应付大多数情况。
我有一个这样的 NSString。
00:03:45
我只想得到这个 03:45 部分并显示在 UILabel 中
我该怎么做?请帮帮我
谢谢
【问题讨论】:
你可以做多种类型
假设这是你的字符串
NSString *origialString =@"00:03:45";
Type-1
NSArray *getBalance = [origialString componentsSeparatedByString: @":"];
origialString = [NSString stringWithFormat:@"%@:%@", [getBalance objectAtIndex:1], [getBalance objectAtIndex:2]];
Type-2
origialString = [origialString substringFromIndex:3];
Type-3
origialString = [origialString substringWithRange:NSMakeRange(3, [origialString length]-3)];
Type-4
// Use NSDateformatter but not necessary for here
【讨论】:
NSArray *timeParts = [string componentsSeparatedByString: @":"];
NSString *newTime = [NSString stringWithFormat:@"%@/%@", [timeParts objectAtIndex:1], [timeParts objectAtIndex:2]];
【讨论】:
不要考虑拆分字符串,考虑格式化恰好是字符串的日期。您需要在此处使用 NSDateFormatter。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];//Or whatever is applicable for your
NSString *timeString = [self.dateFormatter dateFromString:[NSDate dateFromString:yourString];
或者,如果您真的想从该特定字符串中获取子字符串,您可以这样做:
NSString *subString = [yourString substringWithRange:NSMakeRange(3, 7)];
【讨论】: