Monday, May 13, 2013

NSString componentsSeparatedByString example ios

[NSString componentsSeparatedByString]
Returns an array containing substrings from the receiver that have been divided by a given separator.
- (NSArray *)componentsSeparatedByString:(NSString *)separator
Parameters
separator
The separator string.
Return Value of [NSString componentsSeparatedByString]
An NSArray object containing substrings from the receiver that have been divided by separator.
Discussion of [NSString componentsSeparatedByString]
The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty.
Example of [NSString componentsSeparatedByString]
NSString *string = @"cxzcvxcv\n14, Beaven Dam Road\nVail, CO81657";
NSString *strAddress = @"14, Beaven Dam Road\nVail, CO81657";
NSLog(@"%@",[string componentsSeparatedByString:strAddress]);

Example of [NSString componentsSeparatedByString]
NSString* myString = @"32.0235, 1.345".
NSArray* myArray = [myString  componentsSeparatedByString:@","];

NSString* firstString = [myArray objectAtIndex:0];
NSString* secondString = [myArray objectAtIndex:1];
Example of [NSString componentsSeparatedByString]
NSString *myString = @"123-456-789-1234-2345-3456-4567";
NSArray *subStrings = [myString componentsSeparatedByString:@"-"];
for (int i = 0; i < [subStrings count]; i++) {
    NSLog(@"string on array position %d is : %@", i, [subStrings objectAtIndex:i]);
}