C语言分割字符串函数strtok

在编程过程中,有时需要对字符串进行分割.而有效使用这些字符串分隔函数将会给我们带来很多的便利.

下面我将在MSDN中学到的strtok函数做如下翻译.

strtok :在一个字符串查找下一个符号

char *strtok( char *strToken, const char *strDelimit );

返回值:返回指向在strToken字符串找到的下一个符号的指针,当在字符串找不到符号时,将返回NULL.每

次调用都通过用NULL字符替代在strToken字符串遇到的分隔符来修改strToken字符串.

参数:
strToken:包含符号的字符串

strDelimit:分隔符集合

注:第一次调用strtok函数时,这个函数将忽略间距分隔符并返回指向在strToken字符串找到的第一个符

号的指针,该符号后以NULL字符结尾.通过调用一系列的strtok函数,更多的符号将从strToken字符串中分

离出来.每次调用strtok函数时,都将通过在找到的符号后插入一个NULL字符来修改strToken字符串.为了

读取strToken中的下一个符号,调用strtok函数时strToken参数为NULL,这会引发strtok函数在已修改过

的strToken字符串查找下一个符号.

Example(摘自MSDN)

/* STRTOK.C: 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>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
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 );
   }
}

Output

A string   of ,,tokens
and some  more tokens

Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens

时间: 2024-08-24 19:02:28

C语言分割字符串函数strtok的相关文章

Sql Server分割字符串函数

-- Description: 分割字符串函数 -- SELECT * FROM dbo.Split('a,b,c,d,e,f,g',',') -- ============================================= CREATE FUNCTION [dbo].[Split] ( @Text VARCHAR(8000) , @Sign NVARCHAR(4000) ) RETURNS @tempTable TABLE ( ID INT IDENTITY(1, 1) PRI

C语言各类字符串函数的实现

C语言各类字符串函数的实现 开学就要开始找工作了,我觉得这些函数被问到的几率还是很大的,所以在这里与大家分享 strlen函数 原型: #include <string.h> unsigned int strlen (char *s); 功能:计算指定的字符串s 的长度,不包括结束字符"\0". 我的实现: /************************************************************************* > File

C语言分割字符串

最近在做一道C语言题目的时候需要用到分割字符串,本来想自己手写的,也不会很麻烦,但想到其他语言都有分割字符串的库函数,C语言怎么会没有呢?所以,在网上搜了一搜,果然有这样的函数,还是很好用的,在此总结. 1 #include <stdio.h> 2 3 #include <string.h> 4 5 int main() 6 { 7 char in[10000]; 8 char delims[] = " "; 9 char *result; 10 11 fget

c语言常见字符串函数经典实现

最近把一些常见的c语言的字符串库函数参照着网上的程序自己实现了一下,也是方便自己复习总结,里面的实现比较经典,下面的函数在我电脑vs2005上都能通过,但未进行严格的测试.点击展开目录,可以直接达到感兴趣的函数实现. /************************************************************************/ /* 1.strcpy函数实现 2.strncpy实现 3.strcat函数实现 4.strncat函数实现 5.strdup实现

strtok()分割字符串函数

#include <iostream> #include <string.h> using namespace std; char *my_strtok(char *dist,const char *src) { static char *result; //此处使用静态变量保存dist, //为了满足库函数strtok(NULL," ")的实现. char map[32]; const char *p = src; memset(map,0,sizeof(ma

VC实现字符串分割的函数strtok

Python网络爬虫-正则表达式 分享第二套C语言源代码合集 vc++6.0如何捕获关机事件并执行我自己的代码 浅谈面向对象--<ThinkinginJava>读书笔记(一) zv5拙侔号http://p.baidu.com/pai/center?uid=e97061626332393034363011aa&type=qq35kx踊泻蓖7tsuhd副战追http://p.baidu.com/pai/center?uid=78e261626333363962616211aa&ty

C++ split分割字符串函数

将字符串绑定到输入流istringstream,然后使用getline的第三个参数,自定义使用什么符号进行分割就可以了. #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; void split(const string& s,vector<int>& sv,const char flag =

c语言中字符串函数的使用

#include<stdio.h> #include<string.h> /* char s1[]="I am a student"; char s2[20]="teacher"; char s3[]="student"; int result; char s4[20],*p; 1.串的长度 int strlen(char *str): printf("%d\n",strlen(s1));//长度为14

SQL分割字符串函数

SQL创建函数 数据库名->可编程性->函数->表值函数 右键创建“多语句表值函数” CREATE FUNCTION [dbo].[fun_splitstr](@str VARCHAR(MAX)) RETURNS @temp TABLE(id INT) AS BEGIN DECLARE @ch AS VARCHAR(100) SET @str+=',' WHILE(@str<>'') BEGIN SET @ch = LEFT(@str,CHARINDEX(',',@str,1