获取子串的索引

发布于 2024-12-05 21:06:56 字数 153 浏览 1 评论 0原文

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

I have char * source, and I want extract from it subsrting, that I know is beginning from symbols "abc", and ends where source ends. With strstr I can get the poiner, but not the position, and without position I don't know the length of the substring. How can I get the index of the substring in pure C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

╭⌒浅淡时光〆 2024-12-12 21:06:56

使用指针减法。

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

Use pointer subtraction.

char *str = "sdfadabcGGGGGGGGG";
char *result = strstr(str, "abc");
int position = result - str;
int substringLength = strlen(str) - position;
手长情犹 2024-12-12 21:06:56

newptr - source 将为您提供偏移量。

newptr - source will give you the offset.

笑红尘 2024-12-12 21:06:56
char *source = "XXXXabcYYYY";
char *dest = strstr(source, "abc");
int pos;

pos = dest - source;
char *source = "XXXXabcYYYY";
char *dest = strstr(source, "abc");
int pos;

pos = dest - source;
<逆流佳人身旁 2024-12-12 21:06:56

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

#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;
}

Here is a C version of the strpos function with an offset feature...

#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;
}
忆梦 2024-12-12 21:06:56

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

  • strlen(substring)将为您提供其长度。
  • substring - source 将为您提供起始索引。

If you have the pointer to the first char of the substring, and the substring ends at the end of the source string, then:

  • strlen(substring) will give you its length.
  • substring - source will give you the start index.
岁月静好 2024-12-12 21:06:56

形式上,其他人是正确的 - substring - source 确实是起始索引。但您不需要它:您可以将其用作 source 的索引。因此,编译器将 source + (substring - source) 计算为新地址 - 但只需 substring 就足以满足几乎所有用例。

只是优化和简化的提示。

Formally the others are right - substring - source is indeed the start index. But you won't need it: you would use it as index into source. So the compiler calculates source + (substring - source) as the new address - but just substring would be enough for nearly all use cases.

Just a hint for optimization and simplification.

城歌 2024-12-12 21:06:56

通过起始词和结束词从字符串中剪切单词的函数

    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
    }

A function to cut a word out out of a string by a start and end word

    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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文