【问题标题】:Detect & remove 'www' in NSURL检测并删除 NSURL 中的“www”
【发布时间】:2018-02-08 01:33:30
【问题描述】:

我有一个 macOS 应用程序。我需要从任何NSURL 中删除方案和www 部分。这是我想出的:

// For testing purposes.
NSString *blogID = @"https://www.testing1234.tumblr.com";

// Create the corrected link string.
NSString *correctLink;

// Convert the string to a url.
NSURL *url = [NSURL URLWithString:blogID];

// Check if the url has a scheme.

if ([url scheme] != nil) {
    correctLink = [url host];
} else {
    correctLink = blogID;
}

// Ensure there are enough characters before
// performing the necessary 'www.' checks.

if ([correctLink length] > 4) {

    // Remove the first instance of 'www.'
    // from the blog url link string.

    if ([[correctLink substringToIndex:4] containsString:@"www."]) {
        correctLink = [correctLink substringFromIndex:4];
    }
}

NSLog(@"correctLink: %@", correctLink);

我的问题是:如何可靠地删除字符串的 www 部分?对于初学者,www 没有不同的变体,例如www2NSURL 是否有任何方法可以让我检测到字符串的 www 部分? (就像我可以通过调用scheme 来检测字符串的http:// 部分一样)。

更新

我不能使用stringByReplacingOccurrencesOfString,因为它会替换所有出现的www。因此,如果 url 恰好是 www.testsitewww.tumblr.com,那么它将变为 testsite.tumblr.com。这不是我想要的,我只想删除第一次出现的www(或任何其他变体,例如www2)。

谢谢,丹。

【问题讨论】:

    标签: objective-c nsstring nsurl host


    【解决方案1】:

    @Supertecnoboff 试试这个方法。 你可以用“。”分隔网址。你可以得到剩余的字符串。

    - 如果是 www。或 www2。或其他任何东西都会用“。”分隔。你可以得到剩余的字符串

    NSString *blogID = @"https://www.testing1234.tumblr.com";
        NSString *correctLink;
        NSURL *url = [NSURL URLWithString:blogID];
        if ([url scheme] != nil) {
            correctLink = [url host];
        } else {
            correctLink = blogID;
        }
        NSArray *arr = [correctLink componentsSeparatedByString:@"."];
        NSString *str = @"";
        for(int i=1; i<arr.count; i++)
        {
            str = [str stringByAppendingString:[arr objectAtIndex:i]];
            if(i != arr.count-1)
                str = [str stringByAppendingString:@"."];
        }
        NSLog(@"correctLink: %@", str); //correctLink: testing1234.tumblr.com
    

    【讨论】:

    • @Supertecnoboff 更新了代码并进行了测试。请检查一下。
    • 完美运行,正是我所需要的。谢谢!
    【解决方案2】:

    这样做

    NSString *blogID = @"https://www.testing1234.tumblr.com";
    [blogID stringByReplacingOccurrencesOfString:@"www" withString:@""];
    //or
    [blogID stringByReplacingOccurrencesOfString:@"www." withString:@""];
    

     if([blogID hasPrefix:@"www"]) {
    //do stuff
     }
    

    【讨论】:

    • 问题在于它将替换所有出现的www。因此,如果 url 恰好是 www.testsitewww.tumblr.com,那么它将变为 testsite.tumblr.com。这不是我想要的,我只想删除第一次出现的www(或任何其他变体,例如www2)。
    • @Supertecnoboff 或者您可以在“.”的基础上使用拆分字符串,并将值传递到数组中。现在您可以遍历数组并根据需要将每个索引处的字符串与“WWW/WW2”进行比较,如果特定索引处的值与“WWW/WW2”匹配,那么您可以保留该索引数据并可以连接其余的索引数据并将值附加到字符串中。这样你的问题就迎刃而解了。
    • 如果你没有得到我的答案,让我再问一遍
    • 见上面的变化
    【解决方案3】:

    您可以替换部分文本例如:

    NSString* url = [urlWithWWW stringByReplacingOccurrencesOfString:@"www." withString:@""];
    

    【讨论】:

    • 问题在于它将替换所有出现的www。因此,如果 url 恰好是 www.testsitewww.tumblr.com,那么它将变为 testsite.tumblr.com。这不是我想要的,我只想删除第一次出现的www(或任何其他变体,例如www2)。
    • 您可以检查文本开头 -> [string hasPrefix:@"www."] -> 然后是子字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2015-08-17
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多