Chinaunix首页 | 论坛 | 博客
  • 博客访问: 493883
  • 博文数量: 51
  • 博客积分: 257
  • 博客等级: 二等列兵
  • 技术积分: 1489
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-02 18:30
个人简介

Unix/Linux, mysql, web,企业应用

文章分类

全部博文(51)

文章存档

2015年(1)

2014年(1)

2013年(48)

2012年(1)

我的朋友

分类: C/C++

2013-03-24 23:55:20

1,字符串连接
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
英文manual并不难,直接上英文:
The  strcat()  function appends the src string to the dest string, over‐writing 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.The strncat() function is similar, except that
       *  it will use at most n characters from src; and
       *  src does not need to be null-terminated if  it  contains  n  or  more characters.
As  with  strcat(),  the  resulting string in dest is always null-terminated. If src contains n or more characters, strncat() writes n+1 characters to dest  (n  from src plus the terminating null byte).  Therefore, the size of dest must be at least strlen(dest)+n+1.
返回值:The strcat() and strncat() functions return a pointer to  the  resulting string dest.
示例代码:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(){
  4.     char *src="world";
  5.     char dest[100]="hello ";
  6.     strcat(dest,src);
  7.     printf("strcat result: %s, strlen: %d\n",dest,(int)strlen(dest));
  8.     return 0;
  9. }
输出:
strcat result: hello world, strlen: 11

2,字符串查找
char *strstr(const char *haystack, const char *needle);
char *strcasestr(const char *haystack, const char *needle);
说明:The strstr() function finds the first occurrence of the substring needle in the string haystack.  The terminating null bytes ('\0') are not  compared. The strcasestr() function is like strstr(), but ignores the case of both arguments.
返回值:These functions return a pointer to the beginning of the  substring,  or NULL if the substring is not found.
示例代码:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(){
  4.     char *src="hello world,I am a strstr example";
  5.     char *psubstr=strstr(src,"am");
  6.     printf("strstr result: %s\n",psubstr);
  7.     return 0;
  8. }
输出:
strstr result: am a strstr example


阅读(6053) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~