您当前的位置: 首页 >  c++

虎冯河

暂无认证

  • 8浏览

    0关注

    112博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ string切割,分解字符串,C 库函数 - strtok()

虎冯河 发布时间:2021-02-26 16:37:02 ,浏览量:8

声明

下面是 strtok() 函数的声明。

char *strtok(char *str, const char *delim)
参数
  • str -- 要被分解成一组小字符串的字符串。
  • delim -- 包含分隔符的 C 字符串。
返回值

该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。

实例

下面的实例演示了 strtok() 函数的用法。

实例
#include  
#include  
int main () 
{ 
    char str[80] = "This is - www.runoob.com - website"; 
    const char s[2] = "-"; char *token; /* 获取第一个子字符串 */ 
    token = strtok(str, s); /* 继续获取其他的子字符串 */ 
    while( token != NULL ) 
    {
         printf( "%s\n", token ); 
         token = strtok(NULL, s); } return(0); 
    }
}

让我们编译并运行上面的程序,这将产生以下结果:

 This is 
 www.runoob.com 
 website

转载自https://www.runoob.com/cprogramming/c-function-strtok.html

关注
打赏
1649935522
查看更多评论
立即登录/注册

微信扫码登录

0.1424s