一、CString之Find()、FindOneOf()、ReverseFind()。此三个函数返回值均为整数int。
1、Find()
该函数从左侧0索引开始,查找第一个出现的字符位置,返回position。示例如下:
CString s( “abcdef” );
ASSERT( s.Find( ‘b’ ) == 1 );
int f = s.Find( “de” ) ; // 结果 f = 3
返回值:
•如果查到,返回以0索引起始的位置
•未查到,返回-1
2、FindOneOf()
给定一字符串,然后查找其中出现的第一个字符位置,示例如下:
CString s( “abcdef” );
ASSERT( s.FindOneOf( “zb” ) == 1 );
返回值:
•如果查到,返回以0索引起始的位置
•未查到,返回-1
3、ReverseFind()
该函数反向(从右向左)查找字符最后一次出现的位置。示例如下:
CString s( “abcd” );
ASSERT( s.ReverseFind( ‘b’ ) == 2 );
返回值:
•如果查到,返回以0索引起始的位置
•未查到,返回-1
二、CString之Left()、Mid()、Right()
查找完成后,我们可能需要截取字符串。CString有如下几个字符串截取函数。
1、Left(int nCount)
该函数截取左侧nCount个字符,如果遇到双字节字符(下面Mid和Right同样理解),比如中文,则可能会截断乱码。因为nCount是按照字节计数的。
2、Mid(int nFirst)和Mid( int nFirst, int nCount)
Mid(int nFirst)函数截取从nFirst开始,直到字符串结束的字符串。
Mid( int nFirst, int nCount)函数则截取从nFirst开始,截取nCount个字节字符。
3、Right(int nCount)
该函数截取右侧nCount个字节字符。
Left()、Mid()、Right()函数示例如下:
CString s=“天缘博客”;//_T(“天缘博客”)
CString s1=s.Left(3);//天?
CString s2=s.Mid(3);//?博客
CString s3=s.Right(3);//?客
s="123456789";
s1=s.Left(3); //123
s2=s.Mid(3); //456789
s3=s.Right(3); //789
三、应用实例
解析一串字符串中的每个数据:
[0,-0.0599377,-0.340784,-0.207604,0.0852548,-0.00647439,0.0075289]
程序如下:
//接收客户端发来的内容,保存在字符数组receiveBuffer中
int socklenServer=sizeof(serveraddr);
int n=recv(m_server,receiveBuffer,sizeof(receiveBuffer),0);
//对接收到的命令进行解析
receiveBuffer[n] = '\0';
CString sTemp;
sTemp.Format("%s",receiveBuffer);
m_ListWords.AddString(sTemp);
m_ListWords.SetTopIndex(m_ListWords.GetCount()-1);
cout<<"n="<<n<<endl;
cout<<"sTemp:"<<sTemp<<endl;
//判断0还是1
if(receiveBuffer[1]=='1')
{
Robot_postion[0]=1;
cout<<"Robot_postion[0]"<<Robot_postion[0]<<endl;
//cout<<"运行跟踪程序"<<endl;
}
else
{
Robot_postion[0]=0;
cout<<"Robot_postion[0]"<<Robot_postion[0]<<endl;
//cout<<"运行初始点对齐程序"<<endl;
}
//实现对机器人发送字符串的解析
int num1 = sTemp.Find(",") ; //找第一个逗号
//cout<<"num1:"<<num1<<endl; //第二位
CString sTemp1 = sTemp.Right(n-num1-1);//对字符串进行第一次截取
//cout<<"sTemp1:"<<sTemp1<<endl;
int n1= sTemp1.GetLength();
//cout<<"第一次截取字符串长度:"<<n1<<endl;
int num2 = sTemp1.Find(",") ; //找第二个逗号
//cout<<"num2:"<<num2<<endl; //第二位
CString sTemp2 = sTemp1.Left(num2);//对字符串第一个数据进行截取
//cout<<"sTemp2:"<<sTemp2<<endl;
Robot_postion[1]=atof(sTemp2);
cout<<"Robot_postion[1]"<<Robot_postion[1]<<endl;
//***********************************************************************************************//
CString sTemp3 = sTemp1.Right(n1-num2-1);//对字符串进行第二次截取
//cout<<"sTemp3:"<<sTemp3<<endl;
int n2= sTemp3.GetLength();
//cout<<"第二次截取字符串长度:"<<n2<<endl;
int num3 = sTemp3.Find(",") ; //找第三个逗号
//cout<<"num3:"<<num3<<endl; //第三位
CString sTemp4 = sTemp3.Left(num3);//对字符串第二个数据进行截取
//cout<<"sTemp4:"<<sTemp4<<endl;
Robot_postion[2]=atof(sTemp4);
cout<<"Robot_postion[2]"<<Robot_postion[2]<<endl;
//***********************************************************************************************//
CString sTemp5 = sTemp3.Right(n2-num3-1);//对字符串进行第三次截取
//cout<<"sTemp5:"<<sTemp5<<endl;
int n3= sTemp5.GetLength();
//cout<<"第三次截取字符串长度:"<<n3<<endl;
int num4 = sTemp5.Find(",") ; //找第四个逗号
//cout<<"num4:"<<num4<<endl; //第四位
CString sTemp6 = sTemp5.Left(num4);//对字符串第三个数据进行截取
//cout<<"sTemp6:"<<sTemp6<<endl;
Robot_postion[3]=atof(sTemp6);
cout<<"Robot_postion[3]"<<Robot_postion[3]<<endl;
//***********************************************************************************************//
CString sTemp7 = sTemp5.Right(n3-num4-1);//对字符串进行第四次截取
//cout<<"sTemp7:"<<sTemp7<<endl;
int n4= sTemp7.GetLength();
//cout<<"第四次截取字符串长度:"<<n4<<endl;
int num5 = sTemp7.Find(",") ; //找第五个逗号
//cout<<"num5:"<<num5<<endl; //第五位
CString sTemp8 = sTemp7.Left(num5);//对字符串第四个数据进行截取
//cout<<"sTemp8:"<<sTemp8<<endl;
Robot_postion[4]=atof(sTemp8);
cout<<"Robot_postion[4]"<<Robot_postion[4]<<endl;
//***********************************************************************************************//
CString sTemp9 = sTemp7.Right(n4-num5-1);//对字符串进行第五次截取
//cout<<"sTemp9:"<<sTemp9<<endl;
int n5= sTemp9.GetLength();
//cout<<"第五次截取字符串长度:"<<n5<<endl;
int num6 = sTemp9.Find(",") ; //找第六个逗号
//cout<<"num6:"<<num6<<endl; //第六位
CString sTemp10 = sTemp9.Left(num6);//对字符串第四个数据进行截取
//cout<<"sTemp10:"<<sTemp10<<endl;
Robot_postion[5]=atof(sTemp10);
cout<<"Robot_postion[5]"<<Robot_postion[5]<<endl;
//***********************************************************************************************//
CString sTemp11 = sTemp9.Right(n5-num6-1);//对字符串进行第六次截取
//cout<<"sTemp11:"<<sTemp11<<endl;
int n6= sTemp11.GetLength();
//cout<<"第六次截取字符串长度:"<<n6<<endl;
CString sTemp12 = sTemp11.Left(n6-1);//对字符串第四个数据进行截取
//cout<<"sTemp12:"<<sTemp12<<endl;
Robot_postion[6]=atof(sTemp12);
cout<<"Robot_postion[6]"<<Robot_postion[6]<<endl;
Finish!
相关链接:
MFC:TCP服务器端程序