`

用strtok()函数提取分隔符隔开的每个字符串

阅读更多

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

 

 


strtok function
char * strtok ( char * str, const char * delimiters );
<cstring>

Split string into tokens

A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes thebeginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.

Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.

Parameters

str
C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters
C string containing the delimiters.
These may vary from one call to another.


Return Value

A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.

Example

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}


Output:
Splitting string "- This, a sample string." into tokens:
This
a
sample
string


See also.
strcspn Get span until character in string (function)
strpbrk Locate character in string (function)

 

分享到:
评论

相关推荐

    C++常用字符串分割方法实例汇总

    参数说明:str为要分解的字符串,delim为分隔符字符串。 返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。 其它:strtok函数线程不安全,可以使用strtok_r替代。 示例: //借助strtok实现...

    使用分隔符解析字符串:这两个函数基于一个或多个分隔符解析字符串或字符串元胞数组。-matlab开发

    这两个函数根据一个或多个分隔符解析字符串(字符数组)或字符串元胞数组。 我将它设计为 strtok.m 的替代品,因为它作为字符串解析函数不符合我的期望或要求。 这些功能无疑需要进一步增强和改进,因此欢迎您提出...

    strtok函数的使用示例

    s为要分解的字符串,delim为分隔符字符串。 例如:”hello,hi:what?is!the.matter;” 把这串字符串传入strtok函数,第二个delim写 “,:?!.;” , 这样就可以得到6个不同的子字符串。 我们来写个例子验证一下,就写...

    C++的字符串分割函数的使用详解

    参数说明:str为要分解的字符串,delim为分隔符字符串。 返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。 其它:strtok函数线程不安全,可以使用strtok_r替代。 示例: //借助strtok...

    C语言切割多层字符串(strtok_r strtok使用方法)

    通过strtok 就可以把3个字符串 “Fred” “John” “Ann”提取出来。 上面的C代码为 代码如下:int in=0;char buffer[]=”Fred,John,Ann”char *p[3];char *buff = buffer;while((p[in]=strtok(buf,”,”))!=NULL)...

    strtok的赞歌.pdf

    标记解析(`Tokenizing`)是最简单也是最常见的解析问题,也就是根据分隔符把一个字符串分割为几个部分。这个定义覆盖了所有这种类型的任务。根据空白分隔符(例如`" \t\n\r"`之一)分割单词。假设有个像`"/usr/...

    c语言 字符串的拼接和分割实例

    1.字符串的拼接 使用c的函数char *strcat(char *str_des, char *str_sou); 将字符串str_sou接在字符串str_des后面(放在str_des的最后字符和“\0”之间)。...(input:字符串,a:分隔符); 之后调用: temp

    C++中strtok()函数的用法介绍

    s为要分解的字符串,delim为分隔符字符串。Description:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则...

    字符串C++整理

    平时对字符串的操作的是很多的...s为要分解的字符串,delim为分隔符字符串。  说明:strtok()用来将字符串分割成一个个片段。当strtok()在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为 字符。在第一

    tokenz:快速通用使用字符串标记器库

    代币 C 中的快速字符串标记器库。 线程安全。 不修改原始字符串。 API 文档位于 tokenz.h 头文件中。 包括在您的项目中 ...使用函数 'tokenzs' 通过字符串分隔符进行标记的示例 char *string = " -=

    allwords:将句子或任何字符串解析为不同的“单词”-matlab开发

    句子解析可以使用 strtok 一次完成一个单词。 但是,有时在一个函数调用中(有效地)将所有单词提取到元胞数组中是有用的。 函数 allwords.m 正是这样做的。 默认情况下,空格、空格(制表符)、回车符和标点字符都...

    tokfun:该程序将输入的字符串简化为句子。-matlab开发

    以下程序将输入的字符串简化为句子。 它还可以用于将字符串简化为单词。... isdelimiter 返回一个矩阵,基本上标识了句号、逗号、空格等分隔符。 该列表可以增加或减少。 token 是 MATLAB 标准函数 strtok 的实现。

Global site tag (gtag.js) - Google Analytics