0

I have a pretty simple question, however I don't know how to approach it off the top of my head. I'd like to get the range of two parts of my NSString.

I've got a time, let's say 9:42, and I want to isolate the NSRange of the hour and minute portion of the string, so the part of the string before the colon, and the part after.

Does anyone have an idea for the best way to approach this? Thanks!

Jano
  • 62,815
  • 21
  • 164
  • 192
mhbdr
  • 753
  • 2
  • 10
  • 26
  • 1
    Have you looked at the [`-[NSString rangeOfString:]` method](http://developer.apple.com/library/ios/Documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-rangeOfString_)? – rob mayoff Apr 06 '13 at 18:49
  • 1
    Is there any way you can get the time string into and NSDate? If so then you can break the date out into components and have easy safe access to the hour and minute. – Dan Fairaizl Apr 06 '13 at 18:54

1 Answers1

9
NSArray *subStrings = [myString componentsSeparatedByString:@":"]; 
NSString *hour = [subStrings objectAtIndex:0];
NSString *minutes = [subStrings objectAtIndex:1];

Hope this helps...

lakshmen
  • 28,346
  • 66
  • 178
  • 276