如何检查NSString是否具有带有特定地址的URL



我一直在搜索和尝试不同的事情,但是短暂地出现了。

我正在尝试将某人复制的URL(即:https://weblite/u/u/drksndrs)与IF语句进行比较,基本上是说复制字符串是否匹配https://weblity/uneting unews/u/prefix,然后继续前进,然后继续前进与程序。根据u/之后,根据其ID搜索不同的用户。这是我现在拥有的代码。

NSString *latest = [UIPasteboard generalPasteboard].string;
NSString *prefix = @"https://www.website.com/u/";
NSRange textRange = [latest rangeOfString:prefix];
if (textRange.location != NSNotFound) {
    NSLog(@"the prefix matches!");
    [self performOperation];
} else {
    NSLog(@"This doesn't match the https://www.website.com/u/ prefix.");
}

您应该检查textRange.location == 0是否是前缀。更好的是,使用hasPrefix:方法。

NSString *latest = [UIPasteboard generalPasteboard].string;
NSString *prefix = @"https://www.website.com/u/";
if ([latest hasPrefix:prefix]) {
    NSLog(@"the prefix matches!");
    [self performOperation];
} else {
    NSLog(@"This doesn't match the https://www.website.com/u/ prefix.");
}

最新更新