I have a string in the following format
myString = \"cat+dog+cow\"
I need to store each string separated by + in to a array. Eg:
You can find this one very simple
NSString *str = @"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:@"+"];
NSLog(@"Array items %@",arr);
OUTPUT:
Array items ( Cat, dog, Cow )
Try This:
NSString *str = @"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:@"+"];
NSLog(@"%@",array) ;
NSArray *strArray = [myString componentsSeparatedByString:@"-"];
firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome
Try this..
NSArray *arr = [myString componentsSeparatedByString:@"-"];
[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome
NSArray *lines = [string componentsSeparatedByString:@"-"];
The first value will be stored in 0th index of lines and second value will be stored in 1th index of lines..
Why not array?
Use the componentsSeparatedByString:
method of NSString.
NSString string = @"hai-welcome";
NSArray myArray = [string componentsSeparatedByString:@"-"];
NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];