C语言 strsep() 用法及其替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6865963/
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
strsep() usage and its alternative
提问by hari
#include <stdio.h>
#include <string.h>
int main() {
char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);
char *token = strsep(&slow_gun, "{");
printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);
return 0;
}
when I execute it:
当我执行它时:
$ cc -o try try_strsep.c
$ ./try
slow_gun: kaliya} [namak]
token: together
But when, I change the char *slogan to:
但是,当我将 char *slogan 更改为:
char *slogan = "kalia} [namak]";
and execute the same program:
并执行相同的程序:
$ vi try_strsep.c
$ cc -o try try_strsep.c
$ ./try
slow_gun: (null)
token: kalia} [namak]
My Questionis, so when I use strsep() and input string does not have the pattern I am looking for, the return of strsep() is wrong. The only way I can validate whether strsep() could not find the pattern is to check if (slow_gun == NUll).
我的问题是,所以当我使用 strsep() 并且输入字符串没有我正在寻找的模式时, strsep() 的返回是错误的。我可以验证 strsep() 是否找不到模式的唯一方法是检查if (slow_gun == NUll).
If I have char *slogan = "together{"then strsepwould successfully return tokenbut returns slow_gunto blank (not null)
如果我有char *slogan = "together{"然后strsep会成功返回token但返回slow_gun空白(不是null)
$ cc -o try try_strsep.c
$ ./try
slow_gun:
token: together
Is there a way I could avoid this IF check and rely on the function to return me the substr and if its not there, return NULL?
有没有办法可以避免这种 IF 检查并依靠该函数将 substr 返回给我,如果它不存在,则返回NULL?
回答by Praetorian
No, there's no way to avoid the check slow_gun == NULL. Here's a descriptionof strsep's behavior:
不,没有办法避免支票slow_gun == NULL。这里有一个描述的strsep的行为:
char *strsep(char **stringp, const char *delim);
DESCRIPTION
If*stringpisNULL, thestrsep()function returnsNULLand does nothing else. Otherwise, this function finds the first token in the string*stringp, where tokens are delimited by symbols in the stringdelim. This token is terminated by overwriting the delimiter with a null byte ('\0') and*stringpis updated to point past the token. In case no delimiter was found, the token is taken to be the entire string*stringp, and*stringpis madeNULL.RETURN VALUE
Thestrsep()function returns a pointer to the token, that is, it returns the original value of*stringp.
说明
如果*stringp是NULL,则strsep()函数返回NULL并且不执行任何其他操作。否则,此函数查找 string 中的第一个标记*stringp,其中标记由 string 中的符号分隔delim。此标记通过用空字节 ('\0')覆盖分隔符而终止,并*stringp更新为指向标记之后。如果未找到分隔符,则将标记视为整个字符串*stringp,并*stringp制作NULL。返回值
该strsep()函数返回一个指向令牌的指针,即返回 的原始值*stringp。
So, if no match is found strsepreturns a pointer to the original string and sets the slow_guninput to NULL.
因此,如果未找到匹配项,则strsep返回指向原始字符串的指针并将slow_gun输入设置为 NULL。
If the delimiter is the last character in the string, that character is overwritten by '\0' and slow_gunis set to the following character, which happens to be the '\0' terminating the original string. This is why print statement prints an empty string.
如果分隔符是字符串中的最后一个字符,则该字符将被 '\0' 覆盖并slow_gun设置为下一个字符,这恰好是终止原始字符串的 '\0'。这就是 print 语句打印空字符串的原因。
NOTEYou're using strdupincorrectly, the caller is responsible for calling freeon the pointer returned by that function.
注意您使用strdup不正确,调用者负责调用free该函数返回的指针。
回答by Michael Burr
the return of strsep() is wrong
strsep() 的返回是错误的
That's not right. strsep()returns the first token it finds - the beginning of the string is by definition the first token. It's just that no delimiter has been found to terminate the token in this case (so the remainder of the string is the token).
那是不对的。 strsep()返回它找到的第一个标记 - 根据定义,字符串的开头是第一个标记。只是在这种情况下没有找到终止标记的分隔符(因此字符串的其余部分是标记)。
strsep()is not intended to be used to 'find a pattern' - it's used to separate tokens based on a set of delimiters. If you want to find a character, use strchr()or strpbrk().
strsep()不打算用于“查找模式” - 它用于根据一组分隔符分隔标记。如果要查找字符,请使用strchr()或strpbrk()。
回答by John Carter
strsep is behaving correctly - from the man page:
strsep 行为正确 - 从手册页:
The
strsep()function locates, in the string referenced by*stringp, the first occurrence of any character in the string delim (or the terminating\0character) and replaces it with a\0. The location of the next character after the delimiter character (orNULL, if the end of the string was reached) is stored in*stringp. The original value of*stringpis returned.
该
strsep()函数在由 引用的字符串中定位*stringp字符串 delim(或终止\0字符)中任何字符的第一次出现,并将其替换为\0。分隔符之后的下一个字符的位置(或者NULL,如果到达字符串的末尾)存储在*stringp. 的原始值*stringp被返回。
The second case iscorrect - since the delimeter isn't found the first parameter is set to point at NULLand the original string is returned. As you say, you need to check for if (slow_gun == NUll)to detect this.
第二种情况是正确的 - 因为没有找到定界符,所以第一个参数被设置为指向NULL并返回原始字符串。正如您所说,您需要检查if (slow_gun == NUll)以检测到这一点。
(incidentally, that's a horribly confusing choice of variable names).
(顺便说一句,这是一个非常令人困惑的变量名称选择)。

