前面已经学习了strcpy和strcmp函数;下面继续学习其他的字符串函数;
strcat(字符数组1,字符数组2)
字符串连接
把两个字符数组中的字符串连接起来,把字符串2连接到字符串1的后面,结果放在字符数组1中;
strlen(字符数组)
获取字符串长度
获取字符串长度,返回值为字符串实际的长度(不包括 '\0' 在内);
strlwr(字符串),转换为小写
strupr(字符串),转换为大写
strstr(字符串1,字符串2)
查找字符串
判断字符串str2是否是str1的子串;如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL;
如果找到该数组,就会从找到的地方开始输出;
vc6新建一个多文档工程;
void CStrtestView::OnDraw(CDC* pDC)
{
CStrtestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str1;
char s1[15] = "hello ";
char s2[15] = "world";
char s3[15] = "AbCddd";
char a[] = "abcdefg";
char b[] = "de";
char b2[] = "xy";
char *ret;
int len;
int ti = atoi(pDoc->GetTitle().Right(1));
switch(ti)
{
case 1:
//pDoc->SetTitle("strcat");
strcat(s1, s2);
pDC->TextOut(50, 50, s1);
break;
case 2:
//pDoc->SetTitle("strlen");
len = strlen(s1);
str1.Format("%d", len);
pDC->TextOut(50, 50, str1);
break;
case 3:
pDC->TextOut(50, 50, strlwr(s3));
break;
case 4:
pDC->TextOut(50, 50, strupr(s3));
break;
case 5:
ret = strstr(a, b);
if (ret == NULL){
pDC->TextOut(50, 50, "Can not find!");
}
else{
pDC->TextOut(50, 50, ret);
}
break;
case 6:
ret = strstr(a, b2);
if (ret == NULL){
pDC->TextOut(50, 50, "Can not find!");
}
else{
pDC->TextOut(50, 50, ret);
}
break;
default:
break;
}
}