C语言函数strtok

C语言中strtok函数的使用教程 在C语言编程中,处理字符串是一项基础且重要的任务。strtok函数就是用于将字符串分割成一个个的标记(tokens)的工具。该函数属于标准库中的string.h头文件,它将字符串按照指定的分隔符进行分割,并能够处理连续的分隔符。基本使用语法如下:其中str是原始待分割的字符串,而delimiters是一个包含所有分隔符的字符串。strtok函数通过在分隔符之间进行迭代,逐个提取标记。重要的是要注意,strtok。 阅读详情
STRTOK(3)                                               Linux Programmer's Manual                                              STRTOK(3)

NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *str, const char *delim);

       char *strtok_r(char *str, const char *delim, char **saveptr);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       strtok_r(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

DESCRIPTION
       The  strtok() function breaks a string into a sequence of zero or more nonempty tokens.  On the first call to strtok() the string to be parsed should be specified in str.  In each subsequent call that should parse the same string, str must be NULL.

        The delim argument specifies a set of bytes that delimit the tokens in the parsed  string.   The  caller  may  specify  different strings in delim in successive calls that parse the same string. 

        Each  call to strtok() returns a pointer to a null-terminated string containing the next token.  This string does not include the delimiting byte.  If no more tokens are found, strtok() returns NULL. 

        A sequence of calls to strtok() that operate on the same string maintains a pointer that determines the point from which to start searching  for the next token.  The first call to strtok() sets this pointer to point to the first byte of the string.  The start of the next token is determined by scanning forward for the next nondelimiter byte in str.  If such a byte is found, it is  taken as  the  start of the next token.  If no such byte is found, then there are no more tokens, and strtok() returns NULL.  (A string that is empty or that contains only delimiters will thus cause strtok() to return NULL on the first call.)

       The end of each token is found by scanning forward until either the next delimiter byte is found or until  the  terminating  null byte ('\0') is encountered.  If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and strtok() saves a pointer to the following byte; that pointer will be used as the starting  point  when  searching  for  the  next token.  In this case, strtok() returns a pointer to the start of the found token.

       From  the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter, and that delimiter bytes at the start or end of the string are  ignored.   Put  another  way:  the tokens  returned  by  strtok() are always nonempty strings.  Thus, for example, given the string "aaa;;bbb,", successive calls to strtok() that specify the delimiter string ";," would return the strings "aaa" and "bbb", and then a null pointer.

       The strtok_r() function is a reentrant version strtok().  The saveptr argument is a pointer to a char *  variable  that  is  used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

       On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored.  In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

       Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

RETURN VALUE
       The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌───────────┬───────────────┬───────────────────────┐
       │Interface  │ Attribute     │ Value                 │
       ├───────────┼───────────────┼───────────────────────┤
       │strtok()   │ Thread safety │ MT-Unsafe race:strtok │
       ├───────────┼───────────────┼───────────────────────┤
       │strtok_r() │ Thread safety │ MT-Safe               │
       └───────────┴───────────────┴───────────────────────┘
CONFORMING TO
       strtok()
              POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.

       strtok_r()
              POSIX.1-2001, POSIX.1-2008.
BUGS
       Be cautious when using these functions.  If you do use them, note that:

       * These functions modify their first argument.

       * These functions cannot be used on constant strings.

       * The identity of the delimiting byte is lost.

       * The strtok() function uses a static buffer while parsing, so it's not thread safe.  Use strtok_r() if this matters to you.

EXAMPLE
       The program below uses nested loops that employ strtok_r() to break a string into a two-level hierarchy  of  tokens.   The  first
       command-line argument specifies the string to be parsed.  The second argument specifies the delimiter byte(s) to be used to sepa‐
       rate that string into "major" tokens.  The third argument specifies the delimiter byte(s) to be  used  to  separate  the  "major"
       tokens into subtokens.

       An example of the output produced by this program is the following:

           $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
           1: a/bbb///cc
                    --> a
                    --> bbb
                    --> cc
           2: xxx
                    --> xxx
           3: yyy
                    --> yyy

   Program source

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

       int
       main(int argc, char *argv[])
       {
           char *str1, *str2, *token, *subtoken;
           char *saveptr1, *saveptr2;
           int j;

           if (argc != 4) {
               fprintf(stderr, "Usage: %s string delim subdelim\n",
                       argv[0]);
               exit(EXIT_FAILURE);
           }

           for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
               token = strtok_r(str1, argv[2], &saveptr1);
               if (token == NULL)
                   break;
               printf("%d: %s\n", j, token);

               for (str2 = token; ; str2 = NULL) {
                   subtoken = strtok_r(str2, argv[3], &saveptr2);
                   if (subtoken == NULL)
                       break;
                   printf(" --> %s\n", subtoken);
               }
           }

           exit(EXIT_SUCCESS);
       }

       Another example program using strtok() can be found in getaddrinfo_a(3).

SEE ALSO
       index(3), memchr(3), rindex(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strstr(3), wcstok(3)

COLOPHON
       This  page  is  part  of  release 4.04 of the Linux man-pages project.  A description of the project, information about reporting
       bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man-pages/.

GNU                                                            2015-08-08                                                      STRTOK(3)

那个说明翻译过来就是这个

函数将字符串分解为一个零个或多个非空令牌的序列。在第一次调用strtok ()时,应该在str中指定要分析的字符串。在每次应该解析同一个字符串的调用中,str必须为null。

delim参数指定一组字节,用于分隔已分析字符串中的标记。调用方可以在解析同一个字符串的连续调用中在delim中指定不同的字符串。

对strtok ()的每个调用返回一个指向包含下一个令牌的空终止字符串的指针。此字符串不包括定界字节。如果未找到更多的令牌, strtok ()将返回null。

在同一个字符串上操作的strtok()调用序列维护一个指针,它确定要从哪个点开始搜索下一个令牌。对strtok ()的第一个调用将此指针设置指向字符串的第一个字节。下一个令牌的开始是通过扫描str中的下一个nondelimiter字节确定的。如果找到了这样的字节,则将其作为下一个令牌的开始。如果未找到此类字节,则不会有更多的令牌, strtok()将返回null。(因此,空或仅包含分隔符的字符串将导致strtok()在第一个调用上返回null。

每个令牌的结尾通过扫描找到,直到找到下一个分隔符字节或遇到终止的空字节( ' \ 0)为止。如果找到了分隔符字节,则用null字节覆盖它来终止当前令牌, strtok()将一个指针保存到以下字节;当搜索下一个令牌时,该指针将用作起始点。在这种情况下, strtok()返回一个指向已找到令牌的开始的指针。

从上面的描述中,它遵循已分析字符串中两个或多个连续分隔符的序列被视为一个分隔符,并且忽略字符串开头或结尾的分隔符字节。换句话说,由strtok()返回的令牌总是非空字符串。例如,给定字符串“AAA”;;BBB ,连续调用指定分隔符字符串的strtok ()。,”将返回字符串“AAA”和“BBB”,然后返回空指针。

strtok()函数是可重入版本strtok()。saveptr参数是指向字符()在内部使用的char*变量的指针,以便在解析同一字符串的连续调用之间保持上下文。

在对strtok()的第一个调用上,str应该指向要分析的字符串,并且忽略saveptr的值。在后续调用中, str应该为null ,saveptr应该自上次调用以来保持不变。

可以使用调用序列()序列同时解析不同的字符串,以指定不同的saveptr参数。

解释

从s开头开始的一个个被分割的串。当查找不到delim中的字符时,返回NULL。
所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

作用

strtok()用来将字符串分割成一个个片段。参数s指向欲分割的 字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的 指针。

strtok函数第一次调用时会把s字符串中所有在delim中出现的字符替换为NULL。然后通过依次调用strtok(NULL, delim)得到各部分子串。

// crt_strtok.c
// compile with: /W3
// 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>
#include <Windows.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char chinese[] = "中国,吃饭";
char *token;

int main( void )
{
    printf( "英文测试:\n" );

    // Establish string and get the first token:
    token = strtok( string, seps ); // C4996
    // Note: strtok is deprecated; consider using strtok_s instead
    while( token != NULL )
    {
        // While there are tokens in "string"
        //这里循环判断返回的token是否为NULL
        //如果不为NULL,继续分割
        printf( " %s\n", token );

        // Get next token: 
        token = strtok( NULL, seps ); // C4996
    }


    printf("原字符串: %s\n", string);   // 结果为 A, 说明原字符串已发生改变


    printf( "\n中文测试:\n");

    token = strtok( chinese, ",");
    while(token != NULL )
    {
        printf( "%s\n", token);

        token = strtok( NULL, ",");
    }

    printf("\n");

}
防火墙学习5---虚拟系统和跟系统通过路由表互访 虚拟系统与跟系统互访可分为虚拟系统访问根系统和跟系统访问虚拟系统。 阅读详情

相关推荐

华硕笔记本+Ubuntu 20.04:用cpupower解决Intel CPU频率上不去/功耗墙问题实战

本文详细介绍了在华硕笔记本Ubuntu 20.04系统下使用cpupower工具解决Intel CPU频率受限和功耗墙问题的实战方法。通过禁用Intel P-State驱动、配置userspace模式以及高级调优技巧,帮助用户突破性能瓶颈,提升编译和计算效率。

weixin_42561476的博客 532

字符串函数和内存函数(下) (字符串查找strstrstrtok,错误信息报告strerror,字符操作内存操作函数memcpy、memmove、memset、memcmp)

在字符串中找一个字符 最后一次 出现的位置int main()//在字符串 abcdebcdf 中字符 d,最后一次 出现的位置printf("找不到\n");2、strtok - 分隔字符串sep参数是个字符串,定义了用作分隔符的字符集合第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。

AZK707的博客 1238

Frida精准定位Shopee x-sap-ri动态签名生成函数

x-sap-ri是Shopee客户端生成的上下文感知型动态令牌,本质为基于设备标识、请求路径与毫秒级时间戳三元组的SHA256哈希值,经Base64编码后作为防重放与防批量伪造的核心校验字段。其生成逻辑深度混淆且跨Java/Native层,静态分析失效,而Frida凭借内存级函数调用拦截与JNI层穿透能力,成为唯一可行的技术路径。本文聚焦Frida在加固App中的实战适配——通过libcrypto.so API特征扫描反向定位被抹名的生成函数,实现对x-sap-ri输入参数(如16字节device_id加密

weixin_33978578的博客 619

C语言函数: 字符串函数及模拟实现strtok()、strstr()、strerror()

作用:字符串查找。在一串字符串中,查找另一串字符串是否存在。 str2在str1中寻找。返回值是char*的指针 原理:如果在str1中找到了str2,则返回在str1中存在的str2的字符串的第一个字符的地址。如果在str1中每找到str2,则返回NULL(空指针)。 a1是在arr1中寻找arr2,在arr1[1]~arr1[4]上找到了与arr2一样的字符串,则返回arr1中字符'b'的地址。 a2是在arr1中寻找arr3,在arr1中并未

srhqwe的博客 1891

airodump-ng、aircrack-ng使用教程

wifi破解、airodump-ng、aircrack-ng脚本

ssmbyss的博客 2251

用STM32的SPI接口读取MT6835磁编码器,手把手教你避开数据读取的坑

本文详细介绍了如何使用STM32的SPI接口读取MT6835磁编码器,并提供了21个实战细节与避坑指南。从硬件连接、SPI配置到数据解析和实时调试,全面解析了读取位置信息过程中的常见问题及解决方案,帮助工程师在工业控制、伺服电机等场景中高效应用AMR技术。

weixin_30729609的博客 1444

字符串分割函数strtok()详解

strtok 函数是 C 语言标准库中的一个用于字符串分割的函数,它通过查找一组指定的分隔符(delimiter characters)来将字符串分割成一系列的子字符串(或称为标记/tokens)。

byte轻骑兵的技术小窝 1万+

投第一篇SCIE英文期刊(详细步骤)

一个投稿SCIE英文期刊的过程

weixin_48849679的博客 1万+

C语言字符串函数大全(全27个函数)

1.函数名: stpcpy 功  能: 拷贝一个字符串到另一个 用  法: char *stpcpy(char *destin, char *source); 程序例: #include  #include  int main(void) {    char string[10];    char *str1 = "abcdefghi";    stpcpy(string, str1); 

BPC's Domain 5352

DDR4协议规范之(一)DDR4结构和寻址

DDR4 SDRAM结构和寻址DDR4 SDRAM的封装和寻址新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 DDR4 SDRAM的封装和寻址 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习

YJFeiii的博客 3万+

23、工业物流中的多智能体路径规划与碰撞处理策略

本文探讨了工业4.0背景下工业物流中的多智能体路径规划(MAPF)与碰撞处理策略。分析了自动导引车(AGV)等移动资源的应用现状,综述了主流路径规划算法如A*、D*Lite、M*等,以及集中式与分布式碰撞检测方法。重点比较了五种碰撞避免策略:路径交叉、优先级、起始时间调整、无交叉路径和速度调整,提出了基于环境条件的策略选择流程,并通过流程图展示了评估、组合、测试与优化的全过程。文章强调应根据订单不确定性、时间约束、优先级和生产组织等因素综合选择或组合策略,以提升物流效率与系统可靠性。

mac99的博客 132

strtok函数

原文http://baike.baidu.com/view/1028553.htm#5 原型   char *strtok(char *s, const char *delim); 功能   分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 说明   strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数d

scoliver的专栏 551

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

c语言中strtok函数_在C语言中使用strtok()和strtok_r()函数

c语言中strtok函数In this article, we’ll take a look at using the strtok() and strtok_r() functions in C. 在本文中,我们将介绍如何在C语言中使用strtok()和strtok_r()函数。 These functions are very useful, if you want to tokenize ...

从零开始的教程世界 3590

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

C语言之strtok函数

【FROM MSDN && 百科】 原型: char *strtok(char *s, const char *delim); #include 分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数

hgj125073的专栏 3万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C语言 strtok字符串分割函数详解

strtok函数的作用是比较独特的,可以用来对字符串进行分割,对于我们获取命令或者数据集合后的数据处理是不可或缺的一步。但是详细介绍strtok的博文很少,而且linux手册的介绍也比较简单,这边写篇博文记录一下学习到的这个函数的用法。

weixin_37704787的博客 1万+

【C语言】strtok、strerror函数

在不同的系统和c语言标准库的实现中都规定了一些错误码,一般是放在errno.h这个头文件中说明的,C语言程序启动的时候就会使用一个全局的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表示没有错误。当我们在使用标准库中的函数的时候发生了某种错误,就会将对应的错误码,存放在errno中,而一个错误码的数字是整数很难理解是什么意思,所以每一个错误码都是。(4)strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。

小何同学的博客 480

C语言strtok函数详解与使用技巧

摘要: strtok是C标准库中的字符串分割函数,通过指定分隔符将字符串拆分为多个标记。其核心特性包括:直接修改原字符串(替换分隔符为\0)、忽略连续分隔符、非线程安全(使用静态变量记录状态)。使用时需首次传入目标字符串,后续调用传入NULL,返回NULL表示结束。线程安全替代方案为strtok_r(POSIX)和strtok_s(C11)。注意事项:空字符串返回NULL、不可嵌套调用、分隔符为字符集合。适用于单线程简单场景,需注意字符串备份和线程安全。

张佳航的博客 1064

C语言strtok函数

strtok是C语言用于分割字符串的函数,需要include<string.h>。 第一次使用时第一个参数传入待分割的字符串,第二个参数传入分割符号。 第二次使用时第一个函数传入NULL,第二个参数传入分割符号。 #include <iostream> #include<cstdio> #include<cstring> using namespace std; int main() { char a[100]; gets(a);

qq_36140649的博客 2360

【C语言】神奇的分割利器:strtok函数解析

strtok函数是C语言中一个用于分割字符串的函数。它被包含于头文件中。它可以将一个字符串按照指定的分隔符进行分割,每次调用返回被分割出的部分,并在内部记录当 前位置,以便下一次调用时能够继续分割。

高耳机的博客 9452

C语言strtok函数的用法

#include int main() { char *s = "[123456,abced,bjhu88a]"; char *start = NULL; char *end = NULL; char dest[100] ; char *ss; start = strchr(s, (int)'['); // 找到字符'['的位置

changzhi的专栏 6305

C语言分解函数strtok

C语言分解函数strtok char *strtok(char *s, char *delim); 功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。 说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。返回指向下一个标记串。当没有标记串时则返回空字...

心中的日月的博客 596

C语言菜鸟入门·字符串分割函数·strtok ()

符串分割函数strtok浅析,一文带你熟悉strtok的用法

时光の尘的博客 3407

C语言strtok()函数用法详解!

strtok是 C 标准库中的字符串分割函数,用于将一个字符串拆分成多个部分(token),以某些字符(称为分隔符)为界限。strNULLdelim" ""/"(斜杠)NULLstrstr'\0'NULLstrtokstrtok会在分隔符处插入'\0',因此原字符串内容会被破坏。如果需要保留原字符串,请先复制到一个新字符串再操作。:由于strtok使用内部静态变量保存状态,它在多线程程序中不安全。多线程环境下,请使用strtok_r。分隔符delim可以包含多个字符,strtok会将所有出现在delim。

weixin_48941116的博客 4844

C语言str系列库函数之strtok()

之所以系统地记下C语言的str系列函数,是因为4月份参加的两次shi

u013071074的专栏 2843
上一篇: C语言函数strcmp
下一篇: C语言中前置++和后置++的区别
isinstance
博客等级 码龄11年 222粉丝 99原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址