读书人

C语言怎么从特殊的地方截取字符串

发布时间: 2013-12-04 17:21:01 作者: rapoo

C语言如何从特殊的地方截取字符串
例如:
有字符串:'VL04a+80ms+VL04b+10ms+VL04c'

我需要将字符串从有加号的地方截开变成5个字符串
那么5个字符串分别是:
'VL04a'
'80ms'
'VL04b'
'10ms'
'VL04c'
请懂的朋友解答下,谢谢!! c语言 字符串 截取
[解决办法]


#include <string.h>
#include <stdio.h>

int main()
{
char text[] = "VL04a+80ms+VL04b+10ms+VL04c";
char* d = "+";
char* p = strtok(text, d);
while(p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, d);
}

return 0;
}



引用:
例如:



有字符串:'VL04a+80ms+VL04b+10ms+VL04c'

我需要将字符串从有加号的地方截开变成5个字符串
那么5个字符串分别是:
'VL04a'
'80ms'
'VL04b'
'10ms'
'VL04c'
请懂的朋友解答下,谢谢!!

[解决办法]
#include <string.h>
#include <stdio.h>

char string[] = "VL04a+80ms+VL04b+10ms+VL04c";
char seps[] = "+";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* 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 );
}
}

[解决办法]
引用:

#include <string.h>
#include <stdio.h>

int main()
{
char text[] = "VL04a+80ms+VL04b+10ms+VL04c";
char* d = "+";
char* p = strtok(text, d);
while(p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, d);
}

return 0;
}


这 无疑是用strtok函数最为方便了!

读书人网 >C语言

热点推荐