how to convert NSString to NSArray [duplicate]

怎甘沉沦 提交于 2019-12-19 12:21:23

问题


I have a string like

NSString* str = @"[90, 5, 6]";

I need to convert it to an array like

NSArray * numbers = [90, 5 , 6];

I did a quite long way like this:


+ (NSArray*) stringToArray:(NSString*)str
{
    NSString *sep = @"[,";
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
    NSArray *temp=[str componentsSeparatedByCharactersInSet:set];

    NSMutableArray* numbers = [[NSMutableArray alloc] init];
    for (NSString* s in temp) {
        NSNumber *n = [NSNumber numberWithInteger:[s integerValue]];
        [numbers addObject:n];
    }

    return numbers;
}

Is there any neat and quick way to do such conversion?

Thanks


回答1:


First remove the unwanted characters from the string, like white spaces and braces:

NSString* str = @"[90, 5, 6]";
NSCharacterSet* characterSet = [[NSCharacterSet
                                 characterSetWithCharactersInString:@"0123456789,"] invertedSet];
NSString* newString = [[str componentsSeparatedByCharactersInSet:characterSet]
                       componentsJoinedByString:@""];

You will have a string like this: 90,5,6. Then simply split using the comma and convert to NSNumber:

NSArray* arrayOfStrings = [newString componentsSeparatedByString:@","];
NSMutableArray* arrayOfNumbers = [NSMutableArray arrayWithCapacity:arrayOfStrings.count];
for (NSString* string in arrayOfStrings) {
    [arrayOfNumbers addObject:[NSDecimalNumber decimalNumberWithString:string]];
}

Using the NSString category from this response it can be simplified to:

NSArray* arrayOfStrings = [newString componentsSeparatedByString:@","];
NSArray* arrayOfNumbers = [arrayOfStrings valueForKey: @"decimalNumberValue"];



回答2:


NSString* str = @"[90, 5, 6]";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"[] "];
NSArray *array = [[[str componentsSeparatedByCharactersInSet:characterSet]
                        componentsJoinedByString:@""]     
                        componentsSeparatedByString:@","];



回答3:


Try like this

NSArray *arr = [string componentsSeparatedByString:@","];



回答4:


NSString *newSTR = [str stringByReplacingOccurrencesOfString:@"[" withString:@""];

newSTR = [newSTR stringByReplacingOccurrencesOfString:@"]" withString:@""];

NSArray *items = [newSTR componentsSeparatedByString:@","];



回答5:


You can achieve that using regular expression 

([0-9]+)

NSError* error = nil;
NSString* str = @"[90, 5, 6]";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];

Then you have a NSArray of string, you just need to iterate it and convert the strings to number and insert them into an array.



来源:https://stackoverflow.com/questions/25501898/how-to-convert-nsstring-to-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!