I have a string "hai-welcome".
I need to split the above string using '-' separator:
mySstring = "hai-welcome" Separate as:
firstString = "hai" secondString = "welcome.
I have a string "hai-welcome".
I need to split the above string using '-' separator:
mySstring = "hai-welcome" Separate as:
firstString = "hai" secondString = "welcome.
You can accomplish this by using "componentsSeparatedByString" method of NSString.
NSString * source = @"hai-welcome";
NSArray * stringArray = [source componentsSeparatedByString:@"-"];
NSString * firstPart = [stringArray objectAtIndex:0]; // Contains string "hai"
NSString * secondPart =[stringArray objectAtIndex:1]; //Contans string "welcome"
NSArray* foo = [@"hai-welcome" componentsSeparatedByString: @"-"];
NSString* first = [foo objectAtIndex: 0];
NSString* second = [foo objectAtIndex: 1];
Try this
NSString *str = @"hai-welcome";
NSArray *listItems = [str componentsSeparatedByString:@"-"];
If you wish to work with other than componentsSeparatedByString
function, I would offer this solution to adopt:
NSString *myString = @"hai-welcome";
NSString *firstString = [myString substringWithRange:NSMakeRange(0, [myString rangeOfString:@"-"].location)];
NSString *secondString = [myString stringByReplacingOccurrencesOfString:[firstString stringByAppendingString:@"-"] withString:@""];