-1

cant seem to be able to get it right..

I am trying to get the range of "Log in" but I just cant figure it out, this is what I am trying so far without result

NSString *alreadyHaveAccount = @"Already have an account? Log in";

const NSRange range = NSMakeRange(5, 7);
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
vzm
  • 2,440
  • 6
  • 28
  • 47
  • 1
    Don't you want `NSRange range = [alreadyHaveAcount rangeOfString:@"Log in"];`? – rmaddy Jul 14 '14 at 04:35
  • @rmaddy no I do, and that works..I am just trying to figure out myself, for my own benefit how to get ranges... i just can't wrap my head around them – vzm Jul 14 '14 at 04:36
  • http://stackoverflow.com/questions/16998478/find-range-of-substring-of-string – Yogendra Jul 14 '14 at 04:36
  • 2
    so you want to know how to build your own implementation of searching substrings? – Andrey Chernukha Jul 14 '14 at 04:37
  • 2
    @vzm Your question is unclear. What exactly don't you understand? What are you trying to do? – rmaddy Jul 14 '14 at 04:38
  • The first argument is `start` and the second is `length`. So you probably want 25 and 6. – jtbandes Jul 14 '14 at 04:39
  • @AndreyChernukha, yes correct – vzm Jul 14 '14 at 04:39
  • @rmaddy, I simply do not understand ranges. I would like to know how to figure out ranges. That is what I do not understand, I want to be able to tell ranges from A TO B by looking at the string. – vzm Jul 14 '14 at 04:40
  • A range is a starting point and a length. That's it. – rmaddy Jul 14 '14 at 04:42
  • Use the range because it is apple struct defined to perform task on string.With NSRange you can do more operation than only search. Read this link for more details: http://nshipster.com/nsrange/ – hahv Jul 14 '14 at 04:44
  • @Rmaddy thank you, sorry for not being clear, but that is exactly what i wanted to know. – vzm Jul 14 '14 at 04:45
  • @jtbandes thank you, that is exactly what i wanted to know. – vzm Jul 14 '14 at 04:45
  • @vzm All you needed to do was read the docs for `NSRange`. – rmaddy Jul 14 '14 at 04:45
  • I think this question is related with this question http://stackoverflow.com/questions/24729244/how-to-get-a-string-to-contain-one-font-for-one-part-and-another-font-for-part – Yogesh Suthar Jul 14 '14 at 04:45

2 Answers2

2

Check this out:

NSRange range = [alreadyHaveAccount rangeOfString:@"Log in"];

if (range.location == NSNotFound) {
    NSLog(@"The string (alreadyHaveAccount) does not contain 'Log in' as a substring");
}
else {
    NSLog(@"Found the range of the substring at (%d, %d)", range.location, range.location + range.length);        
}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
Usman Nisar
  • 3,031
  • 33
  • 41
1

Use the NSString method - (NSRange)rangeOfString:(NSString *)aString.

In your case, the implementation would look like this.

NSString *alreadyHaveAccount = @"Already have an account? Log in";
NSRange rangeOfLogIn = [alreadyHaveAcount rangeOfString:@"Log in"];

Edit: If you wanted help creating your own method, you should have specified that.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Brian Tracy
  • 6,801
  • 2
  • 33
  • 48