C/C++中字符串截取的函数
2014-02-25 16:54
330 查看
用strtok函数,其函数声明为
strDelimit Set of delimiter characters
返回值说明 All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
例子如下:
char *strtok( char *strToken, const char *strDelimit );
在C++中应该有更好的方法,比如MFC中CString类中的成员函数SpanExcluding和[b]SpanIncluding都能达到截取字符串的作用。[/b]
[b]下面主要介绍strtok。strtok,[/b]SpanExcluding和SpanIncluding都可以在MSDN中找到详细的应用方法
参数
strTokenString containing token(s)
strDelimit Set of delimiter characters
返回值说明 All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
例子如下:
Example
/* 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
相关文章推荐
- access截取字符串,Left 函数,Right 函数
- php截取中文字符串长度的mb_substr()、mb_strcut()函数 附strlen/mb_strlen
- [C++]C++ 函数指针 实现 函数名字符串 到 函数调用 cmath.h
- 关于c++中字符串的系统函数
- Mysql字符串截取函数SUBSTRING的用法说明
- thinkphp内置截取字符串函数无法显示省略号解决方法
- php截取中文字符串函数实例
- 包含中英文的字符串截取算法,与php系统字符串截取函数
- MySQL 字符串截取函数
- 支持汉字的字符串截取函数 substr_for_gb2312
- bat 批处理 字符串 替换函数 和 字符串截取函数
- (实用篇)php中计算中文字符串长度、截取中文字符串的函数代码
- PHP 字符串编码截取函数(兼容utf-8和gb2312)
- js字符串截取函数slice()、substring()、substr()
- 10、 java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况
- 完整中文字符串截取长度函数
- C/C++字符串查找函数
- JavaScript中字符串截取函数slice()、substring()、substr()
- Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数
- thinkphp 内置字符串截取函数