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

I want to get NSMutableArray from these numbers. How can I do this?

DrummerB
  • 39,814
  • 12
  • 105
  • 142
ttotto
  • 826
  • 3
  • 13
  • 31
  • could you provide code snip for me? – ttotto Mar 20 '13 at 14:21
  • you got the answer from DrummerB – Anoop Vaidya Mar 20 '13 at 14:22
  • today only similar question...http://stackoverflow.com/questions/15515905/extract-digits-from-string-in-obj-c/15516109#15516109 – Anoop Vaidya Mar 20 '13 at 14:22
  • 2
    You shoould not edit your question, once answers are there. Then answer may not match with your edited question. – Anoop Vaidya Mar 20 '13 at 14:35
  • I tried to post question code from paste, \n were removed in the original question. – ttotto Mar 20 '13 at 14:39
  • But still, what @AnoopVaidya said. Don't change relevant information in your question once there are answers (or at all actually). Either add an "EDIT:" tag and add new information below or ask a new question. – DrummerB Mar 20 '13 at 14:43
  • 1
    That looks as if you had stored an array into a string using the `numberVector = [myArray description]` method or `numberVector = [NSString stringWithFormat:@"%@", myArray]`. Perhaps there is a better solution than to store the array in a string? – Martin R Mar 20 '13 at 14:44
  • 1
    AS Martin said, if this is the case, you are rotating around for no reason. – Anoop Vaidya Mar 20 '13 at 14:46
  • once check my answer it'l helps you ,in that you can give any type of string it gives an array with the numbers only. – Balu Mar 20 '13 at 14:50

5 Answers5

3

This is the simpler one, convert it to json string first then convert is to array using 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];

Update

This will work for both @"( 1, 2, 3, 4)" and @"(\n 1,\n 2,\n 3,\n 4\n)" as json string can have new line and spaces.

P.S This will work for iOS 5.0 or greater for other iOS you can use SBJSON or other parsing library available.

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
1

If you know that this is exactly your format and don't have to be flexible in the amount of spaces, brackets or commas:

NSCharacterSet *trimSet = [NSCharacterSet characterSetWithCharactersInString:@" ()"];
numberVector = [numberVector stringByTrimmingCharactersInSet:trimSet];
NSArray *numbers = [numberVector componentsSeparatedByString:@", "];
DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

try like this it'l works fine for any type of data it accepts only numbers.

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);
Balu
  • 8,470
  • 2
  • 24
  • 41
0

See the code below, it should work:

  NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" ()"];  
  numberVector = [numberVector stringByTrimmingCharactersInSet:cSet];
  //specify delimiter below
  NSArray *numbers = [numberVector componentsSeparatedByString:@", "];
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
-2

With this:

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

EDIT

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:@", "]
CainaSouza
  • 1,417
  • 2
  • 16
  • 31