一些字符串函数的用法

1. strdup()
strdup()       Returns a pointer to a new string which is a duplicate
               of the 
string to which s1 points.  The space for the
               
new string is obtained using the malloc() function (see
               malloc(3C)).
char *strdup(const char *s)
{
        
char *= NULL;
        
if (s && (t = (char*)malloc(strlen(s) + 1)))
        strcpy(t, s);
        
return t;
}
 

strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干,在strdup中分配的内存要使用者自己释放。

2. strchr()

The strchr() function searches for the first occurrence of a string inside another string.
strchr()函数的作用是:返回一个字符串在另一个字符串中首次出现的位置到后者末尾的子字符串(大小写不敏感)。

This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
如果这个函数执行成功,将返回剩余字符串(存在相匹配的字符);如果没有找到相匹配的字符,则反会False。

This function is an alias of the strstr() function.
这个函数的功能与strstr()函数相类似。


3. strnicmp()

函数原型:extern int strnicmp(char *str1,char * str2,int n)

                    或者extern int strncmpi(char *str1,char * str2,int n)

参数说明:str1为第一个要比较的字符串,str2为第二个要比较的字符串,n为指定字符串str1和str2进行比较的字符的个数。
       
所在库名:#include <string.h>
 
函数功能:比较字符串str1和str2的前n个字符串字典序的大小,但是不区分字母大小写。
 
返回说明:返回整数值:当str1<str2时,返回值<0; 当str1=str2时,返回值=0; 当str1>str2时,返回值>0。



posted on 2009-09-23 09:50  race604  阅读(279)  评论(0编辑  收藏  举报

导航