如何拆分 iOS 中的字符串

How to spliit a NSString in iOS

我有一个这样的 NSString。

00:03:45

我只想得到这个 03:45 部分并在 UILabel

中显示

我该怎么做?请帮助我

谢谢

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)];

你可以做多种类型

假定这是您的字符串

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