最近公司的项目用MFC做的,碰到到很多字符串处理问题,用CString类处理起来总感觉力不从心,于是,总结了C语言中常用的字符处理函数,留作备用。
先贴出string.h头文件的内容,这个来自MSDN。
#if <TRADITIONAL C HEADERS>
#include <string.h>
namespace std {
using ::memchr;
using ::memcmp;
using ::memcpy;
using ::memmove;
using ::memset;
using ::size_t;
using ::strcat;
using ::strchr;
using ::strcmp;
using ::strcoll;
using ::strcpy;
using ::strcspn;
using ::strerror;
using ::strlen;
using ::strncat;
using ::strncmp;
using ::strncpy;
using ::strpbrk;
using ::strrchr;
using ::strspn;
using ::strstr;
using ::strtok;
using ::strxfrm;
}
#endif
介绍上面这些函数之前,先介绍几个上面没有的。
bcmp
原型:extern int bcmp(const void *s1, const void *s2, int n); 用法:#include <string.h> 功能:比较字符串s1和s2的前n个字节是否相等 说明:如果s1=s2或n=0则返回零,否则返回非零值。bcmp不检查NULL。
// bcmp.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Golden Global View";
char *s2="Golden global view";;
clrscr(); // clear screen
if(!bcmp(s1,s2,7))
printf("s1 equal to s2 in first 7 bytes");
else
printf("s1 not equal to s2 in first 7 bytes");
getchar();
clrscr();
if(!bcmp(s1,s2,12))
printf("s1 equal to s2 in first 12 bytes");
else
printf("s1 not equal to s2 in first 12 bytes");
getchar();
return 0;
}
bcopy
原型:extern void bcopy(const void *src, void *dest, int n); 用法:#include <string.h> 功能:将字符串src的前n个字节复制到dest中 说明:bcopy不检查字符串中的空字节NULL,函数没有返回值。
// bcopy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20];
clrscr(); // clear screen
bcopy(s,d,6);
printf("s: %s\n",s);
printf("d: %s\n",d);
getchar();
clrscr();
s[13]=0;
bcopy(s+7,d,11); // bcopy ignore null in string
printf("%s\n",s+7);
for(i=0;i<11;i++)
putchar(d[i]);
getchar();
return 0;
}
bzero
原型:extern void bzero(void *s, int n); 用法:#include <string.h> 功能:置字节字符串s的前n个字节为零。 说明:bzero无返回值。
// bzero.c
#include <syslib.h>
#include <string.h>
main()
{
struct
{
int a;
char s[5];
float f;
} tt;
char s[20];
bzero(&tt,sizeof(tt)); // struct initialization to zero
bzero(s,20);
clrscr();
printf("Initail Success");
getchar();
return 0;
}
memicmp
原型:extern int memicmp(void *buf1, void *buf2, unsigned int count); 用法:#include <string.h> 功能:比较内存区域buf1和buf2的前count个字节但不区分字母的大小写。 说明:memicmp同memcmp的唯一区别是memicmp不区分大小写字母。 当buf1<buf2时,返回值<0 当buf1=buf2时,返回值=0 当buf1>buf2时,返回值>0
// memicmp.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Hello, Programmers!";
char *s2="Hello, programmers!";
int r;
clrscr();
r=memicmp(s1,s2,strlen(s1));
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");
getchar();
return 0;
}
memchr
原型:extern void *memchr(void *buf, char ch, unsigned count); 用法:#include <string.h> 功能:从buf所指内存区域的前count个字节查找字符ch。 说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
// memchr.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Hello, Programmers!";
char *p;
clrscr();
p=memchr(s,'P',strlen(s));
if(p)
printf("%s",p);
else
printf("Not Found!");
getchar();
return 0;
}
memcmp
原型:extern int memcmp(void *buf1, void *buf2, unsigned int count); 用法:#include <string.h> 功能:比较内存区域buf1和buf2的前count个字节。 说明: 当buf1<buf2时,返回值<0 当buf1=buf2时,返回值=0 当buf1>buf2时,返回值>0
// memcmp.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Hello, Programmers!";
char *s2="Hello, programmers!";
int r;
clrscr();
r=memcmp(s1,s2,strlen(s1));
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");
getchar();
return 0;
}
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
// memcpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20];
clrscr();
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s",d);
getchar();
return 0;
}
memset
原型:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>
功能:把buffer所指内存区域的前count个字节设置成字符c。
说明:返回指向buffer的指针。
// memset.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
clrscr();
memset(s,'G',6);
printf("%s",s);
getchar();
return 0;
}
strtok
原型:extern char *strtok(char *s, char *delim);
用法:#include <string.h>
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
// strtok.c
#include <syslib.h>
#include <string.h>
#include <stdio.h>
main()
{
char *s="Golden Global View";
char *d=" ";
char *p;
clrscr();
p=strtok(s,d);
while(p)
{
printf("%s\n",s);
strtok(NULL,d);
}
getchar();
return 0;
}
strcspn
原型:extern int strcspn(char *s1,char *s2); 用法:#include <string.h> 功能:在字符串s1中搜寻s2中所出现的字符。 说明:返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。
// strcspn.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char *r="new";
int n;
clrscr();
n=strcspn(s,r);
printf("The first char both in s1 and s2 is: %c",s[n]);
getchar();
return 0;
}
strpbrk
原型:extern char *strpbrk(char *s1, char *s2);
用法:#include <string.h>
功能:在字符串s1中寻找字符串s2中任何一个字符相匹配的第一个字符的位置,空字符NULL不包括在内。
说明:返回指向s1中第一个相匹配的字符的指针,如果没有匹配字符则返回空指针NULL。
// strpbrk.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Welcome To Beijing";
char *s2="BIT";
char *p;
clrscr();
p=strpbrk(s1,s2);
if(p)
printf("%s\n",p);
else
printf("Not Found!\n");
p=strpbrk(s1, "Da");
if(p)
printf("%s",p);
else
printf("Not Found!");
getchar();
return 0;
}
另外常用的还有
atof | converts a string to a double |
atoi | converts a string to an integer |
atol | converts a string to a long |
isalnum | true if a character is alphanumeric |
isalpha | true if a character is alphabetic |
iscntrl | true if a character is a control character |
isdigit | true if a character is a digit |
isgraph | true if a character is a graphical character |
islower | true if a character is lowercase |
isprint | true if a character is a printing character |
ispunct | true if a character is punctuation |
isspace | true if a character is a space character |
isupper | true if a character is an uppercase character |
isxdigit | true if a character is a hexidecimal character |
strstr | finds the first occurance of a substring of characters |
strtod | converts a string to a double |
strtok | finds the next token in a string |
strtol | converts a string to a long |
strtoul | converts a string to an unsigned long |
strxfrm | converts a substring so that it can be used by string comparison functions |
tolower | converts a character to lowercase |
toupper | converts a character to uppercase |
其他更多内容,可以参考的string.h说明http://www.cplusplus.com/reference/clibrary/cstring/