1.strrchr函数 / strchr函数 /strstr函数
strrchr()函数的作用是:查找一个字符在另一个字符串中末次出现的位置,
并返回字符串中的这个位置的指针,可以输出从这个位置起一直到字符串结束的所有字符。
如果未能找到指定字符,那么函数将返回NULL。
函数原型是: char *strrchr(char *str, char c);
举个例子:
return 0;
}
//要想得到前面那部分可以这样改
int main()
{
char s[]="C://Program Files//Borland//Delphi7//Bin//delphi32.exe";
char *p=strrchr(s,'//');
*(p+1)='/0';
cout<<s; //输出C:/Program Files/Borland/Delphi7/Bin/
return 0;
}
strchr函数:
与strrchr函数不同的是,它是查找一个字符在另一个字符串中首次出现的位置,相同的是它也是返回
字符串中这个位置的指针。
函数原型:char *strchr(char *s,char c);
例子:
char *a="I am chinese !";
char *m=strchr(a,'e');
cout<<m<<endl;
return 0;
}
strstr函数
功能:与strchr函数有点类似的是strstr函数是查找子一个字符串在另一个字符串中首次出现的位置,而strchr是查找一个字
符在另一个字符串中首次出现的位置,这点从函数名上也可以看的出来。最后都是返回该位置的指针。
原型:char *strstr(char *a, char *b);
例子:
char *a="I am chinese !";
char *m=strstr(a,"am");
cout<<m<<endl;
return 0;
}
2.substr函数
功能 :返回一个从指定位置开始,并具有指定长度的子字符串。
函数原型:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
这个函数是string类的方法,所以要用string字符串来调用,由于一般用char*的情况也很多
于是下面例子故意加上了这两种风格字符串相互转化的情况。
例子:
3.find函数
功能:假如有两个字符串A和B,要查找A字符串中是否包含有B字符串就可以用这个函数。不包含的时候
函数返回-1.否则就返会字符串A中所包含的字符串B的第一个字符的索引。
函数原型:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
举个例子:
int main()
{
char *a="I am chinese !";
char *b="chinese";
string s=a;
int k;
k=s.find(b,0);//以第二个为例子,其他都一样
cout<<k<<endl;
return 0;
}
4.strncat函数 / strcat函数
原型:char *strncat(char *dest,char *src,int n);
用法:#include <string.h>
功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'/0')并添加'/0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
strncat函数的例子:
char a[20]="I am chinese ";
char *b="people";
char *m=strncat(a,b,3);
cout<<m<<endl;
return 0;
}
strcat函数:是strncat函数的弱化版
原型: char *strcat(char *dest,char *src);
用法:#include <string.h>
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'/0')并添加'/0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
例子:
char a[20]="I am chinese ";
char *b="people";
char *m=strcat(a,b);
cout<<m<<endl;
return 0;
}
5.memcpy函数 / strcpy函数 /memcmp函数
memcpy函数
原型:void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
例子:
return 0;
}
strcpy函数
原型:char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
例子:
return 0;
}
memcmp函数
原型:int memcmp(void *buf1, void *buf2, unsigned int count);
用法:#include <string.h>
功能:比较内存区域buf1和buf2的前count个字节
说明:buf1>buf2时,返回值>0.一般为1
buf1<buf2时,返回值<0.一般为-1
buf1=buf2时,返回值=0.
例子:
int main()
{
char a[10]="aaa",b[10]="bbb";
if(memcmp(a,b,10)>0)
cout<<"a>b"<<endl;
else
if(memcmp(a,b,10)<0)
cout<<"a<b"<<endl;
else
cout<<"a=b"<<endl;
return 0;
}
6.memset函数 / bzero函数
memset函数
原型:void *memset(void *buffer, char c, int count);
用法:#include <string.h>
功能:把buffer所指内存区域的前count个字节设置成字符c。
说明:返回指向buffer的指针。
例子:
return 0;
}
bzero函数
原型:void bzero(void *s, int n);
用法:#include <string.h>
功能:置字节字符串s的前n个字节为零。
说明:bzero无返回值。
例子:
#define bzero(a,b) memset(a,0,b)
/*由于bzero函数是在linux平台下用的,直接在VC6.0里用会报错,所以提前定义一下它,不为别的,就为展示下它的功能*/
int main()
{
char a[]="I am chinese !";
bzero(a+4,5);
cout<<a<<endl;
return 0;
}
7.strtok函数
原型:char *strtok(char *s, char *delim);
用法:#include <string.h>
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在s中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
例子:
char a[]="192.168.0.1";
char*p=strtok(a,".");//此时p为192
while(p!=NULL) //挨个把后面的IP输出
{
cout<<p<<" ";
p=strtok(NULL,"."); //注意设置为NULL
}
return 0;
}//最后输出为192 168 0 1
就先写这么多了,以后碰到了再补上