-1

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

chipbk10
  • 5,783
  • 12
  • 49
  • 85

5 Answers5

13

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"];
Community
  • 1
  • 1
redent84
  • 18,901
  • 4
  • 62
  • 85
5
NSString* str = @"[90, 5, 6]";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"[] "];
NSArray *array = [[[str componentsSeparatedByCharactersInSet:characterSet]
                        componentsJoinedByString:@""]     
                        componentsSeparatedByString:@","];
Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • why don't you just _sepatatedByString_ with `@", "`? anyway (+1) because that could be the most light-hearted and working solution. – holex Aug 26 '14 at 09:20
  • @holex Yeah, we can add `whitespace` with `NSCharacterSet` as i edited `@"[] "` will be good and thanks a lot for your comment. – Buntylm Aug 26 '14 at 09:31
2

Try like this

NSArray *arr = [string componentsSeparatedByString:@","];
Ganesh Kumar
  • 708
  • 6
  • 25
  • but I still have "[", and another thing is the returned array contains NSString elements instead of NSInteger elements – chipbk10 Aug 26 '14 at 09:11
  • `[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];` `[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]];` ;) – geo Aug 26 '14 at 09:13
  • @chipbk10 use like this NSArray *Arr = [Str componentsSeparatedByString:@","]; [Arr objectAtIndex:0];//90 [Arr objectAtIndex:1];//5 [Arr objectAtIndex:2];//6 – Ganesh Kumar Aug 26 '14 at 09:23
1
NSString *newSTR = [str stringByReplacingOccurrencesOfString:@"[" withString:@""];

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

NSArray *items = [newSTR componentsSeparatedByString:@","];
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0

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.

gimpycpu
  • 607
  • 6
  • 16