【问题标题】:Remove http:// from NSString从 NSString 中删除 http://
【发布时间】:2011-11-03 08:30:35
【问题描述】:

如何从NSString 中删除某些文本,例如“http://”?它必须完全按照这个顺序。感谢您的帮助!

这是我正在使用的代码,但是 http:// 没有被删除。相反,它显示为http://http://www.example.com。我该怎么办?谢谢!

NSString *urlAddress = addressBar.text;
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
urlAddress = [NSString stringWithFormat:@"http://%@", addressBar.text];
NSLog(@"The user requested this host name: %@", urlAddress);

【问题讨论】:

  • 您再次添加相同的字符串。如果要删除 http://,则不需要第 3 行
  • 如果用户没有输入 http://,那么 UIWebView 将起作用。但是,如果用户确实输入了 http://,那么 UIWebView 将无法工作。通过删除任何 http:// 然后重新插入一次,我可以保证 NSString 看起来像这样:http://www.google.com

标签: iphone objective-c ios ipad nsstring


【解决方案1】:

这将删除任何方案,包括 http、https 等。

NSRange dividerRange = [str rangeOfString:@"://"];
NSString *newString = [str substringFromIndex:NSMaxRange(dividerRange)];

【讨论】:

    【解决方案2】:

    由于线程仍然处于活动状态并出现在我的搜索中以从 URL(不是 NSString)中删除前缀...如果您从 URL 开始,则有一个单行:

    String(url.absoluteString.dropFirst((url.scheme?.count ?? -3) + 3))
    

    【讨论】:

      【解决方案3】:

      对于那些使用 swift 并且已经到达这里的人。

      extension String {
      
         func withoutHttpPrefix() -> String {
             var idx: String.Index?;
             if self.hasPrefix("http://www.") {
                idx = self.index(startIndex, offsetBy: 11)
             } else if hasPrefix("https://www.") {
                 idx = self.index(startIndex, offsetBy: 12)
             } else if self.hasPrefix("http://") {
                idx = self.index(startIndex, offsetBy: 7)
             } else if hasPrefix("https://") {
                 idx = self.index(startIndex, offsetBy: 8)
             }
      
             if idx != nil {
                 return String(self[idx!...])
             }
             return self
         }
      }
      

      【讨论】:

        【解决方案4】:

        大家好,有点晚了,但我有一个通用的方法 比方说:

        NSString *host = @"ssh://www.somewhere.com";
        NSString *scheme = [[[NSURL URLWithString:host] scheme] stringByAppendingString:@"://"]; 
        // This extract ssh and add :// so we get @"ssh://" note that this code handle any scheme http, https, ssh, ftp ....
        NSString *stripHost = [host stringByReplacingOccurrencesOfString:scheme withString:@""]; 
        // Result : stripHost = @"www.somewhere.com"
        

        【讨论】:

          【解决方案5】:

          一种更通用的方式:

          - (NSString*)removeURLSchemeFromStringURL:(NSString*)stringUrl {
              NSParameterAssert(stringUrl);
              static NSString* schemeDevider = @"://";
          
              NSScanner* scanner = [NSScanner scannerWithString:stringUrl];
              [scanner scanUpToString:schemeDevider intoString:nil];
          
              if (scanner.scanLocation <= stringUrl.length - schemeDevider.length) {
                  NSInteger beginLocation = scanner.scanLocation + schemeDevider.length;
                  stringUrl = [stringUrl substringWithRange:NSMakeRange(beginLocation, stringUrl.length - beginLocation)];
              }
          
              return stringUrl;
          }
          

          【讨论】:

            【解决方案6】:

            斯威夫特 3

            用于替换所有匹配项:

            let newString = string.replacingOccurrences(of: "http://", with: "")
            

            用于替换字符串开头的匹配项:

            let newString = string.replacingOccurrences(of: "http://", with: "", options: .anchored)
            

            【讨论】:

              【解决方案7】:

              如果你想修剪两边并减少代码:

              NSString *webAddress = @"http://www.google.co.nz";
              
              // add prefixes you'd like to filter out here
              NSArray *prefixes = [NSArray arrayWithObjects:@"https:", @"http:", @"//", @"/", nil];
              
              for (NSString *prefix in prefixes)
                  if([webAddress hasPrefix:prefix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:prefix withString:@"" options:NSAnchoredSearch range:NSMakeRange(0, [webAddress length])];
              
              // add suffixes you'd like to filter out here
              NSArray *suffixes = [NSArray arrayWithObjects:@"/", nil];
              
              for (NSString *suffix in suffixes)
                  if([webAddress hasSuffix:suffix]) webAddress = [webAddress stringByReplacingOccurrencesOfString:suffix withString:@"" options:NSBackwardsSearch range:NSMakeRange(0, [webAddress length])];
              

              此代码将删除前面的指定前缀和后面的后缀(如尾部斜杠)。只需将更多子字符串添加到前缀/后缀数组即可过滤更多。

              【讨论】:

                【解决方案8】:

                这是一个处理 http 和 https 的解决方案:

                    NSString *shortenedURL = url.absoluteURL;
                
                    if ([shortenedURL hasPrefix:@"https://"]) shortenedURL = [shortenedURL substringFromIndex:8];
                    if ([shortenedURL hasPrefix:@"http://"]) shortenedURL = [shortenedURL substringFromIndex:7];
                

                【讨论】:

                  【解决方案9】:

                  NSString* newString = [string stringByReplacingOccurrencesOfString:@"http://" withString:@""];

                  【讨论】:

                    【解决方案10】:

                    这是另一种选择;

                    NSMutableString *copiedUrl = [[urlAddress mutablecopy] autorelease];
                    [copiedUrl deleteCharactersInRange: [copiedUrl rangeOfString:@"http://"]];
                    

                    【讨论】:

                      【解决方案11】:

                      或

                      +(NSString*)removeOpeningTag:(NSString*)inString tag:(NSString*)inTag {
                          if ([inString length] == 0 || [inTag length] == 0) return inString;
                          if ([inString length] < [inTag length]) {return inString;}
                          NSRange tagRange= [inString rangeOfString:inTag];   
                          if (tagRange.location == NSNotFound || tagRange.location != 0) return inString; 
                          return [inString substringFromIndex:tagRange.length]; 
                      }
                      

                      【讨论】:

                        【解决方案12】:

                        如果 http:// 在你可以使用的字符串的开头

                         NSString *newString  = [yourOriginalString subStringFromIndex:7];
                        

                        否则按照 SVD 的建议

                        编辑:看到问题后编辑

                        改变这一行

                        [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
                        

                        到

                        urlAddress  = [urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
                        

                        【讨论】:

                          【解决方案13】:

                          另一种方法是:

                          NSString *str = @"http//abc.com";  
                          NSArray *arr = [str componentSeparatedByString:@"//"];  
                          NSString *str1 = [arr objectAtIndex:0];       //   http  
                          NSString *str2 = [arr objectAtIndex:1];       //   abc.com
                          

                          【讨论】:

                            【解决方案14】:
                            NSString *newString = [myString stringByReplacingOccurrencesOfString:@"http://"
                                                                                      withString:@""
                                                                                         options:NSAnchoredSearch // beginning of string
                                                                                           range:NSMakeRange(0, [myString length])]
                            

                            【讨论】:

                              【解决方案15】:

                              像这样?

                              NSString* stringWithoutHttp = [someString stringByReplacingOccurrencesOfString:@"http://" withString:@""];
                              

                              (如果您只想删除开头的文本,请执行 jtbandes 所说的操作 - 上面的代码也将替换字符串中间的出现)

                              【讨论】:

                              • @Jack Humphries - Krishnabhadra 是对的,当您执行 stringByReplacingOccurrencesOfString 时,您没有分配 urlAddress 的更改值:
                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2010-10-29
                              • 1970-01-01
                              • 2011-01-08
                              • 1970-01-01
                              • 2011-11-29
                              相关资源
                              最近更新 更多