我如何找到字符串的范围?

时间:2014-07-14 04:34:04

标签: ios objective-c

似乎无法做到正确..

我正在努力获得"登录的范围"但我无法弄明白,这是我到目前为止没有结果的尝试

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

const NSRange range = NSMakeRange(5, 7);

2 个答案:

答案 0 :(得分:2)

检查出来:

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);        
}

答案 1 :(得分:1)

使用NSString方法- (NSRange)rangeOfString:(NSString *)aString

在您的情况下,实现将如下所示。

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

编辑:如果您想要帮助创建自己的方法,您应该指定它。