5 个解决方案
#1
试试函数strpbrk。
(Scan strings for characters in specified character sets.)
还好函数比较好用。
(Scan strings for characters in specified character sets.)
还好函数比较好用。
#2
// crt_strtok.c
/* In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
Tokens:
A
string
of
tokens
and
some
more
tokens
/* In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
Tokens:
A
string
of
tokens
and
some
more
tokens
#3
函数strtok()按照第二个参数中指定的字符把一个字符串分解为若*分。函数strtok()具有“破坏性”,它会在原字符串中插入NUL字符(如果原字符串还要做其它的改变,应该拷贝原字符串,并将这份拷贝传递给函数strtok())。函数strtok()是不能“重新进入”的,你不能在一个信号处理函数中调用strtok()函数,因为在下一次调用strtok()函数时它总是会“记住”上一次被调用时的某些参数。strtok()函数是一个古怪的函数,但它在分解以逗号或空白符分界的数据时是非常有用的。
#4
strtok是正解。但这个函数也有很多意想不到的坏处。
#5
有意思,我待伙作一下
#1
试试函数strpbrk。
(Scan strings for characters in specified character sets.)
还好函数比较好用。
(Scan strings for characters in specified character sets.)
还好函数比较好用。
#2
// crt_strtok.c
/* In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
Tokens:
A
string
of
tokens
and
some
more
tokens
/* In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
Tokens:
A
string
of
tokens
and
some
more
tokens
#3
函数strtok()按照第二个参数中指定的字符把一个字符串分解为若*分。函数strtok()具有“破坏性”,它会在原字符串中插入NUL字符(如果原字符串还要做其它的改变,应该拷贝原字符串,并将这份拷贝传递给函数strtok())。函数strtok()是不能“重新进入”的,你不能在一个信号处理函数中调用strtok()函数,因为在下一次调用strtok()函数时它总是会“记住”上一次被调用时的某些参数。strtok()函数是一个古怪的函数,但它在分解以逗号或空白符分界的数据时是非常有用的。
#4
strtok是正解。但这个函数也有很多意想不到的坏处。
#5
有意思,我待伙作一下