【问题标题】:How to parse numbers from NSString?如何从 NSString 解析数字?
【发布时间】:2013-03-20 14:18:52
【问题描述】:
NSString *numberVector = @"( 1, 2, 3, 4)";

我想从这些数字中获取 NSMutableArray。 我该怎么做?

【问题讨论】:

  • 你能为我提供代码片段吗?
  • 你从 DrummerB 那里得到了答案
  • 你不应该编辑你的问题,一旦有答案。那么答案可能与您编辑的问题不匹配。
  • 这看起来就像您使用numberVector = [myArray description] 方法或numberVector = [NSString stringWithFormat:@"%@", myArray] 将数组存储到字符串中一样。也许有比将数组存储在字符串中更好的解决方案?
  • 正如马丁所说,如果是这样的话,你会无缘无故地旋转。

标签: iphone objective-c nsstring


【解决方案1】:

这个比较简单,先转成json字符串再转成数组用NSJSONSerialization

NSString *numberVector = @"( 1, 2, 3, 4)";
numberVector = [numberVector stringByReplacingOccurrencesOfString:@"(" withString:@"["];
numberVector = [numberVector stringByReplacingOccurrencesOfString:@")" withString:@"]"];
NSError* error;
NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:[numberVector dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];

更新

这对@"( 1, 2, 3, 4)"@"(\n 1,\n 2,\n 3,\n 4\n)" 都有效,因为 json 字符串可以有新行和空格。

P.S 这适用于 iOS 5.0 或更高版本的其他 iOS,您可以使用 SBJSON 或其他可用的解析库。

【讨论】:

  • NSJSONSerialization ios >5.0(适用于 iOS 5.0 及更高版本。)
  • @EvgeniyS for
【解决方案2】:

如果您知道这正是您的格式,并且不必在空格、括号或逗号的数量上保持灵活:

NSCharacterSet *trimSet = [NSCharacterSet characterSetWithCharactersInString:@" ()"];
numberVector = [numberVector stringByTrimmingCharactersInSet:trimSet];
NSArray *numbers = [numberVector componentsSeparatedByString:@", "];

【讨论】:

    【解决方案3】:

    像这样尝试,它适用于它只接受数字的任何类型的数据。

    NSString *numberVector = @"(\n 1,\n 2,\n 3,\n 4\n)";
    
        NSString *onlyNumbers = [numberVector  stringByReplacingOccurrencesOfString:@"[^0-9,]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [numberVector  length])];
        NSArray *numbers=[onlyNumbers componentsSeparatedByString:@","];
        NSLog(@"%@",numbers);
    

    【讨论】:

      【解决方案4】:

      请看下面的代码,它应该可以工作:

        NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" ()"];  
        numberVector = [numberVector stringByTrimmingCharactersInSet:cSet];
        //specify delimiter below
        NSArray *numbers = [numberVector componentsSeparatedByString:@", "];
      

      【讨论】:

        【解决方案5】:

        有了这个:

        NSString *numberVector = @"( 1, 2, 3, 4)";
        

        编辑

        NSError *error = NULL;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&([^;])*;" options:NSRegularExpressionCaseInsensitive error:&error];
        NSString *modifiedString = [numberVector stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
        
        NSArray *listItems = [[modifiedString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsSeparatedByString:@", "]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多