【问题标题】:C beginner: split a stringC初学者:拆分字符串
【发布时间】:2011-12-05 15:28:40
【问题描述】:

我有以下字符串:"http://www.google.ie/"。我想创建一个字符串"www.google.ie"

如何在 C 中做到这一点?到目前为止,这是我尝试过的:

char* url="http://www.google.ie/";
char* url_stripped=NULL;
char* out=strtok(url,"http://");
while(out){
    out=strtok(0,".");
    url_stripped=out;
    break;
}
printf("%s\n",url_stripped);

但它不起作用。我也担心如果我有一个包含“h”、“t”、“t”或“p”的网址,事情会变得一团糟。

我还需要能够从一开始就删除“https://”。

【问题讨论】:

  • 尝试 strstr() 也可以:)
  • 如果要在第二个斜线处拆分,为什么要在.上进行分词?
  • stackoverflow.com/questions/726122/…(特别是bortzmeyer的回答)
  • 除了其他人提到的所有其他内容之外,您不得将指向字符串文字的指针作为第一个参数传递给strtok,因为strtok 在地点。

标签: c string split


【解决方案1】:

C 库为您提供了很多功能! 因此,建议首先在这里查看:http://www.cplusplus.com/reference/clibrary/cstring/,以便您可以选择适合您需求的功能。 而不是重新发明我建议与您合作的算法! 干得好!

【讨论】:

    【解决方案2】:

    如何检查字符串是否以"http://""https://"开头,然后跳过七八个字符,然后搜索第一个'/'

    char *url="http://www.google.ie/";
    char *tmp = url;
    char *stripped_url;
    
    if (strncmp(tmp, "http://", 7) == 0 || strncmp(tmp, "https://", 8) == 0)
        tmp += (tmp[4] == 's') ? 8 : 7;  /* Skip over the "http://" or "https://" */
    
    char *slash = strchr(tmp, '/');
    if (slash != NULL)
        stripped_url = strndup(tmp, slash - tmp);  /* slash-tmp is the length between start of the string and the slash */
    else
        stripped_url = strdup(tmp);
    
    printf("domain name = \"%s\"\n", strupped_url);
    
    free(stripped_url);
    

    【讨论】:

      【解决方案3】:

      您应该使用 / 进行标记化

      char url[]="http://www.google.ie/";
      char* url_stripped=strtok(url,"/");
      url_stripped=strtok(NULL,"/");
      printf("%s\n",url_stripped);
      

      【讨论】:

      • 这是第三个反斜杠,所以如果你想使用 strrok,请重复第二行到最后一行。
      • 您的代码声明了两次url_stripped,却忘记了冒号后面有一个双反斜杠。
      • @gnometorule strtok 在重复令牌时负责处理令牌
      • @phresnel 如果令牌连续重复多次,strtok 会处理这个
      • @Mansuro:这对我来说很新鲜;必须说我觉得它违反直觉。这使得 strtok 对于许多解析情况来说是不可取的,因为似乎没有办法检查有多少部分被跳过(尽管我承认在这种情况下这种行为是首选的)。修复后收到我的 +1。
      【解决方案4】:

      实际上有很多方法可以做到这一点。您并没有真正具体说明代码通常应该做什么。如,你想在这个字符串中隔离什么: “http://stackoverflow.com/questions/8387669/c-beginner-split-a-string/”

      无论如何,如果您只是想丢失那个“http://”和最后一个“/”,我建议使用以下代码:

      char url[] = "http://www.google.ie/";
          char url_stripped[100];
          sscanf(url, "http://%s", url_stripped);//get new string without the prefix "http://"
          url_stripped[strlen(url_stripped)-1] = '\0';//delete last charactar (replace with null terminator)
          printf("%s\n",url_stripped);
      

      “sscanf”函数在这种情况下会非常方便。它的工作原理很像“fscanf”和“scanf”,但输入是字符串。 至于“char url_stripped[100];”这一行确保您有足够的空间或使用 malloc(strlen(url)+1);和免费();当您不再需要该字符串时。

      【讨论】:

        【解决方案5】:

        一个迟到的解决方案:

        const char* PROTOCOLS[]  = { "http://", "https://", 0 };
        char* url_stripped = 0;
        const char* protocol;
        char* url = *(a_argv + 1);
        
        for (size_t i = 0; 0 != PROTOCOLS[i]; i++)
        {
            protocol = strstr(url, PROTOCOLS[i]);
            if (protocol == url) /* Ensure starts with and not elsewhere. */
            {
                const char* first_fwd_slash;
                protocol += strlen(PROTOCOLS[i]);
        
                first_fwd_slash = strchr(protocol, '/');
                if (0 == first_fwd_slash)
                {
                    url_stripped = strdup(protocol);
                }
                else
                {
                    const size_t size = first_fwd_slash - protocol + 1;
                    url_stripped = malloc(sizeof(char) * size);
                    memcpy(url_stripped, protocol, size - 1);
                    *(url_stripped + size - 1) = 0;
                }
                break;
            }
            url_stripped = 0;
        }
        
        if (0 != url_stripped)
        {
            printf("[%s]\n", url_stripped);
            free(url_stripped);
        }
        

        【讨论】:

          【解决方案6】:

          我有以下字符串:“http://www.google.ie/”我想创建 一个字符串“www.google.ie”

          你可以这样做(更少的代码,最大的速度):

          #define protocol  "http://"
          #define host      "www.google.ie"
          #define slash     "/"
          
          // "http://www.google.ie/"
          printf("Whole string: %s\n", protocol host slash);
          
          // "www.google.ie"
          printf("URL only: %s\n", host);
          

          简单吧?

          【讨论】:

          • 谢谢,为了正确起见,我将 'url' 替换为 'host'。
          【解决方案7】:
          char* url="http://www.google.ie/";
          char* url_stripped;
          strcpy(url_stripped,url+7);
          printf("%s\n",url_stripped);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多