0

这里的任何人都知道如何将这个数组分成2个?

2013-08-25 02:47:47.052 yahoo[11357:c07] (
"",
"1377253260000.33300.0",
"1377253440000.33280.0",
"1377254100000.33280.0",
"1377255600000.33220.0",
"1377257400000.33220.0",
"1377261660000.33200.0",
"1377264000000.33200.0",
"1377264060000.33200.0",
"1377267780000.33200.0",
"1377271260000.33200.0",
"1377273120000.33200.0",
"1377273180000.33200.0",
"1377273240000.33240.0",
""
)

第一个 NSArray 将带有长数字,第二个带有较小的数字,包括“。”。

就像这样:array1 与 1377253260000 和 array2 与 33300.0 等等。

4

2 回答 2

1

有很多不同的方法可以做到这一点。例如,你可以做一些简单的事情,比如找到第一个句点,然后将字符串添加到第一个数组中的那个句点,然后在下一个数组中添加所有内容:

NSMutableArray *smallerNumbers = [NSMutableArray array];
NSMutableArray *longNumbers    = [NSMutableArray array];

for (NSString *string in array) {
    NSRange range = [string rangeOfString:@"."];
    if (range.location != NSNotFound) {
        [longNumbers    addObject:[string substringToIndex:range.location - 1]];
        [smallerNumbers addObject:[string substringFromIndex:range.location + 1]];
    } else {
        [longNumbers    addObject:@""];    // or you could insert [NSNull null] or whatever
        [smallerNumbers addObject:@""];
    }
}
于 2013-08-25T06:27:09.067 回答
0

另一种方式..

NSArray *objects = @[
                    @"",
                    @"1377253260000.33300.0",
                    @"1377253440000.33280.0",
                    @"1377254100000.33280.0",
                    @"1377255600000.33220.0",
                    @"1377257400000.33220.0",
                    @"1377261660000.33200.0",
                    @"1377264000000.33200.0",
                    @"1377264060000.33200.0",
                    @"1377267780000.33200.0",
                    @"1377271260000.33200.0",
                    @"1377273120000.33200.0",
                    @"1377273180000.33200.0",
                    @"1377273240000.33240.0",
                    @""
                    ];

NSMutableArray *firstParts = [[NSMutableArray alloc] initWithCapacity:objects.count];
NSMutableArray *secondParts = [[NSMutableArray alloc] initWithCapacity:objects.count];

for (NSString *object in objects)
{
    NSArray *components = [object componentsSeparatedByString:@"."];

    if (components.count > 0) {
        [firstParts addObject:components[0]];
    }
    if (components.count > 1) {
        [secondParts addObject:components[1]];
    }
}
NSLog(@"firstParts = %@", firstParts);
NSLog(@"secondParts = %@", secondParts);
于 2013-08-25T06:39:06.880 回答