linux初探

首页

应用服务器

Linux技巧

中文文档

Linux初级

服务器源代码

命令详解

Linux技术应用

Linux安全应用

Linux业界新闻

UniX技术文章

Linux编程与内核

Linux数据库

Linux服务器

Linux安装指导

Linux论坛

首页>>Linux编程与内核>>文章正文

C++常用字符串处理函数及使用示例

char *strcpy(char *s1, const char *s2)
将字符串s2复制到字符串数组s1中,返回s1的值

char *strncpy(char *s1, const char *s2, size_t n)
将字符串s2中最多n个字符复制到字符串数组s1中,返回s1的值

char *strcat(char *s1, const char *s2)
将字符串s2添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值

char *strncat(char *s1, const char *s2, size_t n)
将字符串s2中最多n个字符添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值

int strcmp(const char *s1, const char *s2)
比较字符串s1和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值

int strncmp(const char *s1, const char *s2, size_t n)
比较字符串s1中的n个字符和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值

char * strtok(char *s1,const char *s2)
用一系列strtok调用将s1字符串标记化(将字符串分成各个逻辑组件,如同一行文本中的每个单词),用字符串s2所包含的字符分隔。首次调用时包含s1为第一个参数,后面调用时继续标记化同一字符串,包含NULL为第一个参数。每次调用时返回当前标记指针。如果函数调用时不再有更多标记,则返回NULL

size_t strlen(const char *s)
确定字符串长度,返回null终止符之前的字符数

使用示例:

//源代码在Visual c++6.0环境下编译通过

#include

#include

int main()

{

char str1[50] = "Happy birthday to ", str2[] = "coffeehu";

char temp1[100],temp2[6], * temp;

char str[] = "This is a sentence with 7 tokens";

strcpy(temp1, str1);

strncpy(temp2, str1, 5);

temp2[5] = '\0';

cout << "strcpy result: " <
cout << "strncpy result: " << temp2 << "\n";

cout << "strcat result: " << strcat(str1, str2) << "\n";

cout << "strncat result: " << strncat(str1, str2, 6) <<"\n";

cout << "strcmp result: " << strcmp(temp2,"Happ") <<"\n";

cout << "strncmp result: " << strncmp(str1,"Happy",5) <<"\n";

//strtok function eg.

temp = strtok(str, " ");

while(temp != NULL)

{

cout << temp <<'\n';

temp = strtok(NULL, " ");

}

cout << "strlen result: " << strlen(str2) <<"\n";

return 0;

}

相关文章

·如何在C语言中巧用正则表达式
·C++编程语言中的四个调试技巧
·用C语言技术进行CGI程序设计
·用C++程序删除文本文件中以“//”开头的行
·用C++ Builder开发多层数据库应用程序
·用C语言实现常见的三种中文内码转换
·C++语言代码检查工具PC-Lint简介
·VC++实现文件夹时间属性的获取与更改
·走进Linux编程的大门

热门文章

·Linux下的编程 PHP高级技巧全
·用Java获得IP地址
·新手学习之浅析一下c/c++中的
·在Linux系统中安装Java
·应用2.6内核超线程模式
·使用TC实现基于linux的流量管
·Linux启动添加内核参数简介
·Linux下的编程 PHP高级技巧全
·在php中避免重复引用的办法
·Linux下的编程 PHP高级技巧全

Copyright@2005 www.linuxGoo.com All Right Reserved