cstring查找字符串(c++ 如何从右边搜索指定字符)

本文目录
- c++ 如何从右边搜索指定字符
- C/C++中怎样从字符串中查找特定的字符
- CString 的find用法
- CString 取字符串
- 怎么用CString 的Find查找中文字符串,并截取
- 如何取得CString中的一段字符串
c++ 如何从右边搜索指定字符
1、如果是MFC的话,可以用CString 类的方法ReverseFind,它返回的索引就是位置。
2、通过STL的string 的 rfind方法。
3、也可以自己写:
int ReverseFind( char* p, char cFind )
{
char* pp = p + strlen(p);
int nIndex = strlen(p);
while( 1 )
{
if( pp == c )
{
return strlen(p) - nIndex;
}
nIndex --;
if( nIndex == 0 )
break;
}
}
C/C++中怎样从字符串中查找特定的字符
读/写方式打开文件,一次读要找的字符串长度的一串字符放在一个字符数组里,与特定的字符串比较,相等时停止读取;然后用随机取存函数将文件指针向负方向移动刚读取的字符串那么长的字符数即定位到了需要的位置。FILE*fp_statfile=fopen("x264_output.txt","rb");CStringfilecontent("");//文件内容读入内存while(!feof(fp_statfile)){filecontent.AppendChar(getc(fp_statfile));}//查找X264的SSIM数值//X264特征字符串CStringfeaturestr("SSIMMeanY:");//查找,返回字符串位置intparaloc=filecontent.Find(featurestr);CStringparastr;//找到了的话if(paraloc!=-1){//跳过特征字符串,提取5位parastr=filecontent.Mid(paraloc+featurestr.GetLength(),5);TRACE("%s\n",parastr);}fclose(fp_statfile);C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛。C++支持多种编程范式——面向对象编程、泛型编程和过程化编程。最新正式标准C++于2014年8月18日公布。其编程领域众广,常用于系统开发,引擎开发等应用领域,是至今为止最受广大程序员受用的最强大编程语言之一。
CString 的find用法
此成员函数用来在此字符串中搜索子字符串的第一个匹配的字符。函数的重载可以接收单个字符(类似于运行时函数strchr)和字符串(类似于strstr)。
示例
//下面演示第一个例子
// CString::Find( TCHAR ch )
CString s( "abcdef" );
int n = s.Find( ’c’ ); // 结果 n = 2
int f = s.Find( "de" ) ; // 结果 f = 3
ASSERT( n == 2 );
ASSERT( f == 3 );
// 下面演示第二个例子
// CString::Find(TCHAR ch,int nStart)
CString str("The stars are aligned");
int n = str.Find(’e’,o); //结果 n = 2
CString 取字符串
帮你写了一个函数:
//通过“,”分割提取字符串
//参数str:被提取的字符串
//参数strArray:提取后的字符串放到这个动态数组中
//返回值:提取出的字符串个数
int ExtString(CString str, CStringArray &strArray)
{
CString strTemp;
int nCount = 0;
strArray.RemoveAll();
while(str.Find(_T(",")) != -1)//查找字符串中是否有“,”出现
{
strTemp = str.Left(str.Find(_T(",")));//提取字符串
str = str.Right(str.GetLength() - str.Find(_T(","))-2);//剩下的字符串
strArray.Add(strTemp);//提取出的字符加入动态数组
nCount++;
}
//最后一个字符串
if(str.GetLength()》0)
{
strArray.Add(str);//提取出的字符加入动态数组
nCount++;
}
return nCount;
}
//这样使用:
CString A = _T("1,abc,ddd,aaa,c,b");
CStringArray strArray; //这是一个动态字符串数组
int n = ExtString(A,strArray);
for(int i=0; i《n; i++)
{//显示出所以字符串
CString str;
str.Format("字符串%d", i+1);
MessageBox(strArray,str);
}
有问题可以百度HI上找我。
怎么用CString 的Find查找中文字符串,并截取
CString 还有 Left(),Mid(),Right()这几个函数哦
比如说想取“中”前面的,就可以 CString temp=str.Left(n)就好了~
如何取得CString中的一段字符串
CString str("12345");
CString test;
test = str.Left(2);//左边两个
AfxMessageBox(test);//12
test = str.Right(2);//右边两个
AfxMessageBox(test);//45
test = str.Mid(2, 3);//索引为2开始的三个
AfxMessageBox(test);//345

更多文章:
writeline教程(.NET对称加密实践 (新手教程))
2025年7月18日 11:00
贝尔摩德小说(女主贝尔摩德的柯南同人文,bg,男主要穿越的)
2025年7月23日 10:00
linux配置python3环境变量(linux执行python脚本)
2025年7月14日 19:15
php内置对象(使用JS,通过.opst把数据交给php处理后返回的数据.)
2025年12月11日 08:45
重写protected方法用什么修饰(这四个修饰符sealed protected private public有什么区别)
2026年3月13日 02:00





![enabled by default(data definition has no type or storage class [enabled by default])](/static/images/nopic/26.jpg)













