首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2013-12-04 
C语言如何从特殊的地方截取字符串例如:有字符串:VL04a+80ms+VL04b+10ms+VL04c我需要将字符串从有加号的

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函数最为方便了!

热点排行