【问题标题】:How to spliit a NSString in iOS [duplicate]如何在 iOS 中拆分 NSString [重复]
【发布时间】:2015-05-08 19:09:46
【问题描述】:

我有一个这样的 NSString。

00:03:45

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

我该怎么做?请帮帮我

谢谢

【问题讨论】:

  • 你可以从实际阅读 NSString 的文档开始。
  • 同意。 Apple 的文档足以应付大多数情况。

标签: ios nsstring


【解决方案1】:

你可以做多种类型

假设这是你的字符串

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 

【讨论】:

    【解决方案2】:
    NSArray *timeParts = [string componentsSeparatedByString: @":"];
    
    NSString *newTime = [NSString stringWithFormat:@"%@/%@", [timeParts objectAtIndex:1], [timeParts objectAtIndex:2]];
    

    【讨论】:

      【解决方案3】:

      不要考虑拆分字符串,考虑格式化恰好是字符串的日期。您需要在此处使用 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)];
      

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2016-02-05
        • 2019-11-18
        • 2010-10-01
        • 1970-01-01
        相关资源
        最近更新 更多