【问题标题】:Get index of substring获取子字符串的索引
【发布时间】:2011-11-21 23:42:37
【问题描述】:

我有char * source,我想从中提取subsrting,我知道它从符号“abc”开始,到source结束的地方结束。使用 strstr 我可以得到指针,但不能得到位置,没有位置我不知道子字符串的长度。如何在纯 C 中获取子字符串的索引?

【问题讨论】:

  • 你可能只用指针就可以做你想做的事,不用担心长度。
  • @Country - 你没有理由不能投票(只是频率可能有限制)

标签: c char substring


【解决方案1】:

使用指针减法。

char *str = "sdfadabcGGGGGGGGG";
char *result = strstr(str, "abc");
int position = result - str;
int substringLength = strlen(str) - position;

【讨论】:

  • 哎呀char *str = "abracabcabcabcabc" :-)
  • 啊,他的“源”字符串以 abc 开头,然后继续... :-)
  • 谢谢大家!由于某种原因我不能投票,所以我只能说声谢谢
【解决方案2】:

newptr - source 会给你偏移量。

【讨论】:

    【解决方案3】:
    char *source = "XXXXabcYYYY";
    char *dest = strstr(source, "abc");
    int pos;
    
    pos = dest - source;
    

    【讨论】:

    • 哎呀source = "abracadabcabcabcabc" :)
    • @pmg - 没关系 - “以 'abc' 开头”仍然会创建正确的结果,因为 strstr() 一旦成功就会停止查找
    • 我会使用 malloc 分配一个数组,只是为了使示例更完整。当然,我也会做一些错误检查;-)
    • @RobertS.Barnes - 你会,但你没有;)
    • 谢谢大家!由于某种原因我不能投票,所以我只能说声谢谢
    【解决方案4】:

    如果你有指向子字符串第一个字符的指针,并且子字符串在源字符串的末尾结束,那么:

    • strlen(substring) 会给你它的长度。
    • substring - source 会给你起始索引。

    【讨论】:

    • 谢谢大家!由于某种原因我不能投票,所以我只能说声谢谢
    【解决方案5】:

    这是带有偏移功能的 C 版本的 strpos 函数...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int strpos(char *haystack, char *needle, int offset);
    int main()
    {
        char *p = "Hello there all y'al, hope that you are all well";
        int pos = strpos(p, "all", 0);
        printf("First all at : %d\n", pos);
        pos = strpos(p, "all", 10);
        printf("Second all at : %d\n", pos);
    }
    
    
    int strpos(char *hay, char *needle, int offset)
    {
       char haystack[strlen(hay)];
       strncpy(haystack, hay+offset, strlen(hay)-offset);
       char *p = strstr(haystack, needle);
       if (p)
          return p - haystack+offset;
       return -1;
    }
    

    【讨论】:

      【解决方案6】:

      形式上其他人是正确的 - substring - source 确实是开始索引。但是您不需要它:您可以将它用作source 的索引。所以编译器将source + (substring - source) 计算为新地址——但对于几乎所有用例来说,substring 就足够了。

      只是优化和简化的提示。

      【讨论】:

      • 谢谢大家!由于某种原因我不能投票,所以我只能说声谢谢
      【解决方案7】:

      通过开头和结尾单词从字符串中切出单词的函数

          string search_string = "check_this_test"; // The string you want to get the substring
          string from_string = "check";             // The word/string you want to start
          string to_string = "test";                // The word/string you want to stop
      
          string result = search_string;            // Sets the result to the search_string (if from and to word not in search_string)
          int from_match = search_string.IndexOf(from_string) + from_string.Length; // Get position of start word
          int to_match = search_string.IndexOf(to_string);                          // Get position of stop word
          if (from_match > -1 && to_match > -1)                                     // Check if start and stop word in search_string
          {
              result = search_string.Substring(from_match, to_match - from_match);  // Cuts the word between out of the serach_string
          }
      

      【讨论】:

      • 问题是关于 C,而不是 C++
      • 在 C++ 中有更简单的方法 - 使用 string::Find 方法和字符串构造函数 string(const string& str, size_t pos, size_t n = npos);
      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 2023-03-11
      • 2010-11-17
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多