是否有内置函数来获取 strsep() 的后半部分?
Is there a built in function to get the back half of a strsep()?
简而言之,目前下面的代码输出:The substring is AES
。不幸的是,我正在寻找结果 The substring is 100
。这是因为 strsep()
只保留拆分的第一部分,而不保留第二部分。有没有办法让第二部分成为保留的部分?
#include <stdio.h>
#include <string.h>
int main () {
const char haystack[50] = "SHA1 = 99\nAES = 100";
const char needle[10] = "Point";
char *ret;
char *bonus;
ret = strstr(haystack, "AES");
bonus = strsep(&ret, "=");
printf("The substring is: %s\n", bonus);
return(0);
}
来自 strsep
function's documentation :
char *strsep(char **stringp, const char *delim);
If *stringp
is NULL, the strsep()
function returns NULL
and does
nothing else. Otherwise, this function finds the first token in the
string *stringp
, that is delimited by one of the bytes in the string
delim
. This token is terminated by overwriting the delimiter with a
null byte ('[=18=]'
), and *stringp
is updated to point past the token.
In case no delimiter was found, the token is taken to be the entire
string *stringp
, and *stringp
is made NULL
.
所以在你的程序中你应该打印 ret
而不是 bonus
:
bonus = strsep(&ret, "=");
printf("The substring is: %s\n", ret);
因为 ret
将指向令牌 "="
。
简而言之,目前下面的代码输出:The substring is AES
。不幸的是,我正在寻找结果 The substring is 100
。这是因为 strsep()
只保留拆分的第一部分,而不保留第二部分。有没有办法让第二部分成为保留的部分?
#include <stdio.h>
#include <string.h>
int main () {
const char haystack[50] = "SHA1 = 99\nAES = 100";
const char needle[10] = "Point";
char *ret;
char *bonus;
ret = strstr(haystack, "AES");
bonus = strsep(&ret, "=");
printf("The substring is: %s\n", bonus);
return(0);
}
来自 strsep
function's documentation :
char *strsep(char **stringp, const char *delim);
If
*stringp
is NULL, thestrsep()
function returnsNULL
and does nothing else. Otherwise, this function finds the first token in the string*stringp
, that is delimited by one of the bytes in the stringdelim
. This token is terminated by overwriting the delimiter with a null byte ('[=18=]'
), and*stringp
is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string*stringp
, and*stringp
is madeNULL
.
所以在你的程序中你应该打印 ret
而不是 bonus
:
bonus = strsep(&ret, "=");
printf("The substring is: %s\n", ret);
因为 ret
将指向令牌 "="
。