c++中用CString处理字符串

本文总结了多种从字符串中提取子串的方法,包括使用MFCCString的Find函数、MFC未公开函数AfxExtractSubString和C语言的strtok函数等,并对比了各自的优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近编程中经常用到字符串处理操作, 其中最多的就是从字符串中取子串了。
今天正好看到这样一个帖子,总结的不错, 引进过来。
------------------
一。前言
程序中经常会遇到要处理用某些符号(如空格,或“, ; . | \t”等)分隔的字符串的问题,我在此做了一些总结和比较。
 
二。处理方法
1. 用MFC CString之Find处理字符串
// 以下方法将一个字符串分解后放到一个CStringArray中:
void ExtractString(CStringArray& arr, const CString strSrc, const CString sep = "\r\n" )
{
 // 预处理: 可根据需要决定是否需要Trim,以及是Trim掉空格/还是分隔符/还是其它
 CString str(strSrc);
 str.TrimLeft(); 
 str.TrimRight();
 
 if(str.IsEmpty())
  return;
 
 // 开始分解
 int pos = str.Find(sep);
 while (pos != -1)
 {
  //if(!str.Left(pos).IsEmpty()) // 如有必要也可在此Trim后再判断是否为空,为空则舍弃
  arr.Add(str.Left(pos));
 
  str = str.Mid(pos + sep.GetLength());
  pos = str.Find(sep);
 }
 
 arr.Add(str); // think
}
 
// BTW,CString提供了TrimLeft和TrimRight,却不能一次TrimAll,并返回Trim后的字符串,我写了一个:
CString TrimAll(CString str, CString strTrim = " ")
{
 str.TrimLeft(strTrim);
 str.TrimRight(strTrim);
 
 return CString(str);
}
 
// 同样是用Find处理字符串,和以上处理方式稍有不同,试比较:
void ExtractString2(CStringArray& arr, const CString strSrc, const CString sep = "\r\n" )
{
  CString str(strSrc);
  if(TrimAll(str).IsEmpty())
  return;
 
 // 开始分解
 int pos = str.Find(sep);
 if(pos == -1) // 未找到分隔符
  arr.Add(str);
 else   // 找到分隔符
 {
  str += sep;    // think
 
  CString s;
  while (pos != -1)
  {
   s = str.Left(pos);
   if(!TrimAll(s).IsEmpty())
    arr.Add(s);
 
   str = str.Mid(pos + sep.GetLength());
   pos = str.Find(sep);
  }
 }
}
 
2. 用MFC未公开函数AfxExtractSubString 处理
// 以下为函数定义及说明:
// AfxExtractSubString 从一个字符(chSep)分隔的字符串(lpszFullString)中取出第iSubString个子串,输出到rString
BOOL AFXAPI AfxExtractSubString (   
    CString& rString,                 // 用于输出子串
    LPCTSTR lpszFullString,     // 被分隔的字符串
    int iSubString,                      // zero-based substring index
    TCHAR chSep = '\n'           // 分隔符
    )
 
// eg:
CString sDesc= "张三|男|28|医生";
CString sOccupation;
if(AfxExtractSubString ( sOccupation, sDesc, 3, '|'))
    cout << "职业:" << sOccupation << endl;
 
3. 用C语言之strtok函数处理
#include <assert.h>
void test()
{
 char* str = "06317377244|13805871280|20040210105049|193|NBGW1|040C|0017|8";
 char seps[]   = ",;|";
 
 char* temp = (char*)malloc(sizeof(char) * (strlen(str)+1));
    strcpy(temp, str);
 
    char* token = strtok(temp, seps);
    while (NULL != token)
    {
  printf("%s\t", token);
  token = strtok(NULL, seps);
    }
 
 free(temp);
}
 
4. 其它方法
当然既然能用MFC的Find函数进行处理,也可能string的find处理,甚至可用最原始的字符比较,再配合一些字串处理函数进行处理。
但我觉得我提供的使用Find类函数处理字串的2种方法非常常用,可供参考。
 
三。比较
处理方法
MFC CString之Find处理字符串 分隔符可是是字符或字串;
MFC中是最常用的方法。
仅限于MFC中使用
用AfxExtractSubString 处理 完全封装,调用简单;
分隔符可是是字符或字串。
仅限于MFC中使用,且未公开;
一般用于取其中某个子串。
用C语言之strtok函数处理 分隔符可以同时指定多个字符,可用于分隔符不是很确定的情况。 分隔符只能是字符;
C库函数,在C兼容
开发环境中通用。
 
四。后记
以上只是我从个人的角度做的一个小结(我用MFC/C++多一些),难免有失偏颇,在BCB/Delphi中肯定也有对应的方式方法,不在本文的讨论范围内,这里就不再赘述。
五。增加
CString 有下面几个函数
Mid      Extracts the middle part of a string (like the Basic MID$ function). 
Left     Extracts the left part of a string (like the Basic LEFT$ function). 
Right     Extracts the right part of a string (like the Basic RIGHT$ function). 
SpanIncluding     Extracts a substring that contains only the characters in a set. 
SpanExcluding    Extracts a substring that contains only the characters not in a set.
点 Mid

CString::Mid 
CString Mid( int nFirst ) const;
throw( CMemoryException );

CString Mid( int nFirst, int nCount ) const;
throw( CMemoryException );

Return Value

A CString object that contains a copy of the specified range of characters. Note that the returned CString object may be empty. 

Parameters

nFirst

The zero-based index of the first character in this CString object that is to be included in the extracted substring.

nCount

The number of characters to extract from this CString object. If this parameter is not supplied, then the remainder of the string is extracted.

Remarks

Extracts a substring of length nCount characters from this CString object, starting at position nFirst (zero-based). The function returns a copy of the extracted substring. Mid is similar to the Basic MID$ function (except that indexes are zero-based).

For multibyte character sets (MBCS), nCount refers to each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.

Example

The following example demonstrates the use of CString::Mid.

// example for CString::Mid
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );
 第2到第4(序号是从0开始的  indexes are zero-based,第2个就是1,2到4总共3个字符)

s,Mid(1,3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值