字符串函数<string.h>

在头文件<string.h>中定义了两组字符串函数。第一组函数的名字以str开头;第二组函数的名字以mem开头。只有函数memmove对重叠对象间的拷贝进行了定义,而其他函数都未定义。比较类函数将其变量视为unsigned char类型的数组。

1 strcpy

#include <string.h>

char *strcpy(char *str1, const char *str2);

把字符串str2(包括'\0')拷贝到字符串str1当中,并返回str1。

2 strncpy

#include <string.h>

char *strncpy(char *str1, const char *str2, size_t count);

把字符串str2中最多count个字符拷贝到字符串str1中,并返回str1。如果str2中少于count个字符,那么就用'\0'来填充,直到满足count个字符为止。

3 strcat

#include <string.h>

char *strcat(char *str1, const char *str2);

把str2(包括'\0')拷贝到str1的尾部(连接),并返回str1。其中终止原str1的'\0'被str2的第一个字符覆盖。

4 strncat

#include <string.h>

char *strncat(char *str1, const char *str2, size_t count);

把str2中最多count个字符连接到str1的尾部,并以'\0'终止str1,返回str1。其中终止原str1的'\0'被str2的第一个字符覆盖。

注意,最大拷贝字符数是count+1。

5 strcmp

#include <string.h>

int strcmp(const char *str1, const char *str2);

按字典顺序比较两个字符串,返回整数值的意义如下:

  • 小于0,str1小于str2;
  • 等于0,str1等于str2;
  • 大于0,str1大于str2;

6 strncmp

#include <string.h>

int strncmp(const char *str1, const char *str2, size_t count);

同strcmp,除了最多比较count个字符。根据比较结果返回的整数值如下:

  • 小于0,str1小于str2;
  • 等于0,str1等于str2;
  • 大于0,str1大于str2;

7 strchr

#include <string.h>

char *strchr(const char *str, int ch);

返回指向字符串str中字符ch第一次出现的位置的指针,如果str中不包含ch,则返回NULL。

8 strrchr

#include <string.h>

char *strrchr(const char *str, int ch);

返回指向字符串str中字符ch最后一次出现的位置的指针,如果str中不包含ch,则返回NULL。

9 strspn

#include <string.h>

size_t strspn(const char *str1, const char *str2);

返回字符串str1中由字符串str2中字符构成的第一个子串的长度。

10 strcspn

#include <string.h>

size_t strcspn(const char *str1, const char *str2);

返回字符串str1中由不在字符串str2中字符构成的第一个子串的长度。

11 strpbrk

#include <string.h>

char *strpbrk(const char *str1, const char *str2);

返回指向字符串str2中的任意字符第一次出现在字符串str1中的位置的指针;如果str1中没有与str2相同的字符,那么返回NULL。

12 strstr

#include <string.h>

char *strstr(const char *str1, const char *str2);

返回指向字符串str2第一次出现在字符串str1中的位置的指针;如果str1中不包含str2,则返回NULL。

13 strlen

#include <string.h>

size_t strlen(const char *str);

返回字符串str的长度,'\0'不算在内。

14 strerror

#include <string.h>

char *strerror(int errnum);

返回指向与错误序号errnum对应的错误信息字符串的指针(错误信息的具体内容依赖于实现)。

15 strtok

#include <string.h>

char *strtok(char *str1, const char *str2);

在str1中搜索由str2中的分界符界定的单词。

对strtok()的一系列调用将把字符串str1分成许多单词,这些单词以str2中的字符为分界符。第一次调用时str1非空,它搜索str1,找出由非str2中的字符组成的第一个单词,将str1中的下一个字符替换为'\0',并返回指向单词的指针。随后的每次strtok()调用(参数str1用NULL代替),均从前一次结束的位置之后开始,返回下一个由非str2中的字符组成的单词。当str1中没有这样的单词时返回NULL。每次调用时字符串str2可以不同。

如:

char *p;

p = strtok("The summer soldier,the sunshine patriot", " ");

printf("%s", p);

do {

p = strtok("\0", ", "); /* 此处str2是逗号和空格 */

if (p)

printf("|%s", p);

} while (p);

显示结果是:The | summer | soldier | the | sunshine | patriot

16 memcpy

#include <string.h>

void *memcpy(void *to, const void *from, size_t count);

把from中的count个字符拷贝到to中。并返回to。

17 memmove

#include <string.h>

void *memmove(void *to, const void *from, size_t count);

功能与memcpy类似,不同之处在于,当发生对象重叠时,函数仍能正确执行。

18 memcmp

#include <string.h>

int memcmp(const void *buf1, const void *buf2, size_t count);

比较buf1和buf2的前count个字符,返回值与strcmp的返回值相同。

19 memchr

#include <string.h>

void *memchr(const void *buffer, int ch, size_t count);

返回指向ch在buffer中第一次出现的位置指针,如果在buffer的前count个字符当中找不到匹配,则返回NULL。

20 memset

#include <string.h>

void *memset(void *buf, int ch, size_t count);

把buf中的前count个字符替换为ch,并返回buf。

c语言字符串库函数#include<string.h>的更多相关文章

  1. c语言实用功能库函数#include<stdlib.h>

    实用函数<stdlib.h> 在头文件<stdlib.h>中说明了用于数值转换.内存分配以及具有其他相似任务的函数. 1 atof #include <stdlib.h& ...

  2. c语言时间库函数#include<time.h>

    日期与时间函数<time.h> 头文件<time.h>中说明了一些用于处理日期和时间的类型和函数.其中的一部分函数用于处理当地时间,因为时区等原因,当地时间与日历时间可能不相同 ...

  3. #include <string.h>

    1 _memccpy 2 _memicmp 3 _strlwr 4 _strrev 5 _strset 6 _strupr 7 memccpy 8 memchr 9 memcpy 10 memicmp ...

  4. 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。

    首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...

  5. 5. 常见C语言字符串库函数的使用及实现

    1. strncat 函数: [函数原型]#include <string.h> char *strncat( char *str1, const char *str2, size_t c ...

  6. #include<string.h>

    #include<string.h> 1 strcpy #include <string.h> char *strcpy(char *str1, const char *str ...

  7. #include<iostream>与#include<iostream.h>以及#inclue<string> 和 #include<string.h>的区别

    转载于:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1900715.html 1.从功能性的角度来讲,<iostream> ...

  8. [C++]在什么时候需要“#include string.h“

    相关资料:https://zhidao.baidu.com/question/515578726.html C++中,string头文件基本上已经包含在iostream中了.但是,平时使用的时候建议加 ...

  9. c语言输入与输出库函数#include<stdio.h>

    last modified: 2010-05-28 输入与输出<stdio.h> 头文件<stdio.h>定义了用于输入和输出的函数.类型和宏.最重要的类型是用于声明文件指针的 ...

随机推荐

  1. 代码静态分析工具PCLint, Splint

    一.PCLint REFER: 代码静态检查工具PC-Lint运用实践 二.Splint 1.在PC-Linux上安装 ①make error undefined reference toyywrap ...

  2. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...

  3. ExecutorService 接口

    先看一个Executor接口,该接口只有一个方法:void execute(Runnable command),用于在未来某个时刻提交一个command,这个command可以被提交到一个新的线程,或 ...

  4. 关于使用工具类org.apache.commons.collections.ListUtils合并List的问题

    今天在做项目时,需要将几个List进行合并,于是就用到了apache提供关于List操作的工具类ListUtils,但是在使用的过程中发现一些问题. public static void main(S ...

  5. RabbitMQ RPC问题

    1.服务器端代码:https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/rpc_server.py 2.客户端代码:htt ...

  6. memcached完全剖析--1

    memcached的基础 翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西很有用,希望大家喜欢. 发表日:2008/7/2 作者:长野雅广(Masahiro Nagan ...

  7. Sublime Text 中使用Git插件连接GitHub

    sublime Text的另一个强大之处在于它提供了非常丰富的插件,可以帮助程序员来适合大多数语言的开发.这些插件通过它自己的Package Controll(包管理)组件来安装,非常方便.一般常用的 ...

  8. KISSY学习笔记(更新中)

    序:身为一个JAVA开发工程师,前端代码我尽量是使用原生的JS来写的,或是使用一些JQ的开源组件(但是也只是使用,没有好好去研究过JQ这个框架).目前由于工作需要,必须要使用KISSY,打算借此机会, ...

  9. Python 基础篇:字符串、列表操作

    字符串操作 判断是否为数字 string = "200" string.isdigit() >>false 待完善.. 列表操作 列表是我们最以后最常用的数据类型之一, ...

  10. Xilium.CefGlue利用XHR实现Js调用c#方法

    防外链 博客园原文地址在这里http://www.cnblogs.com/shen6041/p/3442499.html 引 Xilium CefGlue是个不错的cef扩展工程,托管地址在这里 ht ...