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:
- Fast enumeration lets you iterate through the members of your
NSArray
- 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 anNSRange
value representing{NSNotFound, 0}
then your string does not contain the character(s) - You can use
-componentsSeparatedByCharactersInSet:
to split each element of the array by your character - 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
2009-11-24 06:56:53
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
2009-11-24 07:24:56
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
2009-11-24 09:50:22
NSLog(@"original string: %@", myString);stringWithout = [myString...];NSLog(@"new string: %@", stringWithout);
David Maymudes
2009-11-24 16:38:02