tags:

views:

56

answers:

1

i have an array of strings say for example @"123",@"373",@"221",@"921" .I need to check in how many elements of that array 2 exists and want to concatenate those elements into a mutable string and finally eradicate all two and prepare a string.I should have a string 13373191 out of the above example

+1  A: 

Instead of doing it for you, here are a few links that will help you figure this out for yourself:

  1. Fast enumeration lets you iterate through the members of your NSArray
  2. If you get back an NSRange value from running -rangeOfCharacterFromSet: on each string, then the string contains the character you're looking for. If you get an NSRange value representing {NSNotFound, 0} then your string does not contain the character(s)
  3. You can use -componentsSeparatedByCharactersInSet: to split each element of the array by your character
  4. You can then use fast enumeration to iterate through each of these split components, using -stringByAppendingString to glue them together

All those links will take you to Apple's documentation that explains each of the concepts and methods.

EDIT - Added Sbrocket's clarification.

Alex Reynolds
Just a note: -rangeOfCharacterFromSet: doesn't return NULL if there is no match, it returns a NSRange value representing {NSNotFound, 0}. Check the documentation page you linked.
Sbrocket
NSString *stringWithout = [myString stringByReplacingOccurencesOfString:@"4" withString:@""];i am using this to separate 4 from my string but cannot find why it s not working
Bapu
NSLog(@"original string: %@", myString);stringWithout = [myString...];NSLog(@"new string: %@", stringWithout);
David Maymudes