获取子字符串的索引

Cou*_*try 23 c substring char

我有char*source,我想从它中提取subsrting,我知道它是从符号"abc"开始,并在源结束时结束.使用strstr我可以得到poiner,但不是位置,没有位置我不知道子串的长度.如何在纯C中获取子字符串的索引?

Rob*_*nes 46

使用指针减法.

char *str = "sdfadabcGGGGGGGGG";
char *result = strstr(str, "abc");
int position = result - str;
int substringLength = strlen(str) - position;
Run Code Online (Sandbox Code Playgroud)


mkb*_*mkb 6

newptr - source 会给你补偿.


Kev*_*imm 5

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

pos = dest - source;
Run Code Online (Sandbox Code Playgroud)