常用字符串处理函数

4 . 6 . 8 . 4 、函数库中库函数的使用
( 1 )gcc中编译链接程序默认是使用动态库的,要想静态链接需要显式用 - static 来强制静态链接。
( 2 )库函数的使用需要注意 3 点:第一,包含相应的头文件;第二,调用库函数时注意函数原型;第三,有些库函数链接时需要额外用 - lxxx来指定链接;第四,如果是动态库,要注意 - L指定动态库的地址。

4 . 6 . 9 . 3 、常用字符串处理函数
( 1 )C库中字符串处理函数包含在string.h中,这个文件在ubuntu系统中在 / usr / include
( 2 )常见字符串处理函数及作用:
  
  
  1. void *memcpy(void *dest, const void *src, size_t n);
  2. //function:The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
  3. //return: a pointer to dest.
  4. //remarks:确定src和dst不会overlap,则使用memcpy效率高
  5. void *memmove(void *dest, const void *src, size_t n);
  6. //function:The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap:copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
  7. //return:returns a pointer to dest.
  8. //remarks:确定会overlap或者不确定但是有可能overlap,则使用memove比较保险
  9. void *memset(void *s, int c, size_t n);
  10. //memory set
  11. //The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。他的填充是以字节为单位,一般用于对字符型变量的初值进行赋值.用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;例:char a[100];memset(a, '/0', sizeof(a));
  12. //return: a pointer to the memory area s.
  13. int memcmp(const void *s1, const void *s2, size_t n);
  14. //memory compare
  15. //The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.(一般用来比较两个字符串是否相等)
  16. //return:当s1<s2时,返回值-1;当s1==buf2时,返回值=0;当buf1>s2时,返回值1
  17. void *memchr(const void *s, int c, size_t n);
  18. //在参数 str 所指向的字符串的前 n 个字节中搜索第一次出现字符 c(一个无符号字符)的位置。
  19. //str -- 指向要执行搜索的内存块。
  20. //c -- 以 int 形式传递的值,但是函数在每次字节搜索时是使用该值的无符号字符形式。
  21. //n -- 要被分析的字节数。
  22. //返回值:该函数返回一个指向匹配字节的指针,如果在给定的内存区域未出现字符,则返回 NULL。
  23. char *strcpy(char *dest, const char *src);
  24. //The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.
  25. //return: a pointer to the destination string dest.
  26. char *strncpy(char *dest, const char *src, size_t n);
  27. // The strncpy() function is similar, except that at most n bytes of src are copied.
  28. //return: a pointer to the destination string dest.
  29. char *strcat(char *dest, const char *src);
  30. //The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.
  31. //return: a pointer to the resulting string dest.
  32. char *strncat(char *dest, const char *src, size_t n);
  33. //The strncat() function is similar, except that it will use at most n bytes from src; and src does not need to be null-terminated if it contains n or more bytes.
  34. //return:a pointer to the resulting string dest.
  35. int strcmp(const char *s1, const char *s2);
  36. //字符串大小的比较是以ASCII 码表上的顺序来决定,此顺序亦为字符的值。strcmp()首先将s1 第一个字符值减去s2 第一个字符值,若差值为0 则再继续比较下个字符,若差值不为0 则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。
  37. int strncmp(const char *s1, const char *s2, size_t n);
  38. //strncmp()首先将s1 第一个字符值减去s2 第一个字符值,若差值为0 则再继续比较下个字符,直到字符结束标志'\0',若差值不为0,则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。
  39. //若str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 若小于s2,则返回小于0的值。
  40. char *strdup(const char *s);
  41. //函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小,然后将参数s 字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。
  42. //返回值:返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL 表示内存不足。
  43. char *strndup(const char *s, size_t n);
  44. //The strndup() function is similar, but copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
  45. char *strchr(const char *s, int c);
  46. //strchr() 将会找出 str 字符串中第一次出现的字符 c 的地址,然后将该地址返回。
  47. //【返回值】如果找到指定的字符则返回该字符所在地址,否则返回 NULL。
  48. char *strstr( char *str, char * substr );
  49. //strstr()函数用来检索子串在字符串中首次出现的位置
  50. //【参数说明】str为要检索的字符串,substr为要检索的子串。
  51. //【返回值】返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。
  52. char * strtok(char *s, const char *delim);
  53. //strtok()用来将字符串分割成一个个片段。参数s 指向欲分割的字符串,参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s 字符串,往后的调用则将参数s 设置成NULL。每次调用成功则返回下一个分割后的字符串指针。
  54. //返回下一个分割后的字符串指针,如果已无从分割则返回NULL。
  55. //用法:http://blog.csdn.net/beyondhaven/article/details/6679206
   
   
  1. int atoi(const char *nptr); //将字符串转为int格式的数
  2. long atol(const char *nptr);
  3. long long atoll(const char *nptr);
  4. long long atoq(const char *nptr);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值