-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);
14
  • 1
    Don't you want NSRange range = [alreadyHaveAcount rangeOfString:@"Log in"];? Commented Jul 14, 2014 at 4: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 Commented Jul 14, 2014 at 4:36
  • stackoverflow.com/questions/16998478/… Commented Jul 14, 2014 at 4:36
  • 2
    so you want to know how to build your own implementation of searching substrings? Commented Jul 14, 2014 at 4:37
  • 2
    @vzm Your question is unclear. What exactly don't you understand? What are you trying to do? Commented Jul 14, 2014 at 4:38

2 Answers 2

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);        
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.