CString字符串查找和截取

一、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服务器端程序

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你要截取两个字符串之间的子串,可以先找到这两个字符串的位置,然后再用 Mid 函数截取它们之间的子串,例如: ```c++ CString str = "Hello <world>!"; int pos1 = str.Find('<'); int pos2 = str.Find('>'); CString substr = str.Mid(pos1 + 1, pos2 - pos1 - 1); // 截取 "world" ``` 这里用了 Find 函数来查找字符在字符串中的位置,然后用 Mid 函数截取它们之间的子串。注意截取的起始位置和长度的计算。如果你要截取的子串包括两个分隔符之间的内容,那么需要根据具体情况调整起始位置和长度的计算。 ### 回答2: 在CString中,可以使用Mid函数来截取两个字符串之间的字符串。Mid函数的用法如下: CString Mid( int nFirst, int nCount ) const; 其中,nFirst表示需要截取的起始位置,nCount表示需要截取的字符数。 假设有一个字符串str,我们想要截取其中的两个字符串之间的字符串,可以先通过Find函数找到两个字符串分别在str中的位置,然后再根据它们的位置和长度来调用Mid函数进行截取。 具体代码如下: CString str = "abcdefg"; CString str1 = "bc"; CString str2 = "ef"; int start = str.Find(str1) + str1.GetLength(); int end = str.Find(str2); CString result = str.Mid(start, end - start); 在上述代码中,我们首先将字符串str定义为"abcdefg",然后定义了两个字符串str1和str2,分别为"bc"和"ef"。然后通过Find函数获得了str1和str2在str中的位置,并将其分别存储在start和end变量中。最后通过调用Mid函数,将起始位置start和截取的字符数(即end - start)作为参数,截取出了两个字符串之间的字符串,并将结果存储在result变量中。 通过上述方式,我们可以在CString中实现截取两个字符串之间的字符串的功能。 ### 回答3: 在CString类中,可以使用Mid函数来截取两个字符串之间的字符串。 Mid函数有多个重载的版本,可以根据不同的需求来选择使用。其中最常用的版本是截取从指定位置开始的一定长度的字符串。 要截取两个字符串之间的字符串,首先需要先确定两个字符串在源字符串中的位置。假设源字符串为str,要截取的两个字符串为str1和str2。可以先使用Find函数来找到str1和str2在字符串str中的索引位置: int index1 = str.Find(str1); //找到str1的索引位置 int index2 = str.Find(str2); //找到str2的索引位置 接下来,可以使用Mid函数来截取两个字符串之间的字符串: CString subStr = str.Mid(index1 + str1.GetLength(), index2 - index1 - str1.GetLength()); 其中,index1 + str1.GetLength()表示从str1的末尾位置开始截取,index2 - index1 - str1.GetLength()表示要截取字符串的长度。 最后,将截取到的子字符串保存到CString类型的subStr变量中。subStr就是两个字符串之间的字符串。 需要注意的是,如果str1或str2不存在于str中,那么Find函数会返回-1,表示未找到相应的字符串。此时需要对找不到字符串的情况进行处理,避免出现错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值