6 strsep
分解字符串
int main (void )
{
/* NAME
strsep - extract token from string
* SYNOPSIS
#include <string.h>
char *strsep(char **stringp, const char *delim);
* DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL and does nothing else.
Otherwise, this function finds the first token in the string *stringp, where tokens
are delimited by symbols in the string delim. This token is terminated with a '\0' character (by overwriting the delimiter)
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.
* RETURN VALUE
The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.
* BUGS
Be cautious when using this function. If you do use it, note that:
* This function modifies its first argument.
* This function cannot be used on constant strings.
* The identity of the delimiting character is lost.
*/
char pSrc[] =".baidu.com.cn.education.mahuateng";
char *pStringgp = pSrc;
char *pRetval = NULL;
while( (pRetval = strsep(&pStringgp, ".")) != NULL )
/* 个人理解:分解字符串为一组字符串, 此函数会改变字符串, 返回开始到 第一个出现 delim 的地方
并用 ‘\0’替换出现的第一个delim子字符串,如果没有 delim,则返回NULL,
此函数 比strtok好处是接受空字符串并且是线程安全的。
切割后源字符串变为第一个delim 到stringp 末尾,并过滤掉第一个delim。strchr函数strchr函数
如果stringp 为NULL,则返回NULL, */
{
printf("[%s]-[%d] pStringgp = %s \n", __func__, __LINE__, pStringgp);
printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29692-6.html
前有周觅
20