0

I have a string "hai-welcome".

I need to split the above string using '-' separator:

mySstring = "hai-welcome" Separate as:

firstString = "hai" secondString = "welcome.

SethO
  • 2,703
  • 5
  • 28
  • 38
pravi
  • 31
  • 1
  • 7

5 Answers5

6

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"
Shaheen M Basheer
  • 1,010
  • 6
  • 11
3

Use NSString's componentsSeparatedByCharactersInSet method

http://www.idev101.com/code/Objective-C/Strings/split.html

black sowl
  • 185
  • 4
3
NSArray* foo = [@"hai-welcome" componentsSeparatedByString: @"-"];
NSString* first = [foo objectAtIndex: 0];
NSString* second = [foo objectAtIndex: 1];
Habib
  • 219,104
  • 29
  • 407
  • 436
3

Try this

NSString *str = @"hai-welcome";
NSArray *listItems = [str componentsSeparatedByString:@"-"];
Ankit Gupta
  • 958
  • 1
  • 6
  • 19
0

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:@""];