C语言 获取子串的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7500892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:41:40  来源:igfitidea点击:

Get index of substring

ccharsubstring

提问by Country

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 strstrI 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?

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

回答by Robert S. Barnes

Use pointer subtraction.

使用指针减法。

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

回答by mkb

newptr - sourcewill give you the offset.

newptr - source会给你抵消。

回答by KevinDTimm

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

pos = dest - source;

回答by Howard J

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

这是具有偏移功能的 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;
}

回答by Graham Borland

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 - sourcewill give you the start index.
  • strlen(substring)会给你它的长度。
  • substring - source会给你开始索引。

回答by glglgl

Formally the others are right - substring - sourceis 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 substringwould be enough for nearly all use cases.

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

Just a hint for optimization and simplification.

只是优化和简化的提示。

回答by StrongLucky

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
    }