VC++中CString里的find函数怎么使用

如题所述

CString::Find
  在一个较大的字符串中查找字符或子字符串
  int Find( TCHAR ch ) const;
  int Find( LPCTSTR lpszSub ) const;
  int Find( TCHAR ch, int nStart ) const;
  int Find( LPCTSTR lpszSub, int nStart ) const;

返回值
  返回此CString对象中与需要的子字符串或字符匹配的第一个字符的从零开始的索引;如果没有找到子字符串或字符则返回-1。

参数
  ch 要搜索的单个字符。
  lpszSub 要搜索的子字符串。
  nStart 字符串中开始搜索的字符的索引,如果是0,则是从头开始搜索。如果nStart不是0,则位于nStart之前的字符不包括在搜索之内。
  pstr 指向要搜索的字符串的指针

说明
  此成员函数用来在此字符串中搜索子字符串的第一个匹配的字符。函数的重载可以接收单个字符(类似于运行时函数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',2); //结果 n = 2

参考资料:http://baike.baidu.com/view/3068605.htm

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-20
CString::Find

int Find( TCHAR ch ) const;
int Find( LPCTSTR lpszSub ) const;
int Find( TCHAR ch, int nStart ) const;
int Find( LPCTSTR lpszSub, int nStart ) const;

返回值:
返回此CString对象中与需要的子字符串或字符匹配的第一个字符的从零开始的索引;如果没有找到子字符串或字符则返回-1。

参数: ch 要搜索的单个字符。
lpszSub 要搜索的子字符串。
nStart 字符串中开始搜索的字符的索引,如果是0,则是从头开始搜索。如果nStart不是0,则位于nStart处的字符不包括在搜索之内。
pstr 指向要搜索的字符串的指针。

说明:
此成员函数用来在此字符串中搜索子字符串的第一个匹配的字符。函数的重载可以接收单个字符(类似于运行时函数strchr)和字符串(类似于strstr)。

示例:
//下面演示第一个例子
// CString::Find( TCHAR ch )
CString s( "abcdef" );
ASSERT( s.Find( 'c' ) == 2 );
ASSERT( s.Find( "de" ) == 3 );

// 下面演示第二个例子
// CString::Find(TCHAR ch,int nStart)
CString str("The stars are aligned");
int n = str.Find('e',5);
ASSERT(n == 12)本回答被网友采纳
第2个回答  2015-10-15
  CString::Find
  在一个较大的字符串中查找字符或子字符串
  int Find( TCHAR ch ) const;
  int Find( LPCTSTR lpszSub ) const;
  int Find( TCHAR ch, int nStart ) const;
  int Find( LPCTSTR lpszSub, int nStart ) const;

  返回值
  返回此CString对象中与需要的子字符串或字符匹配的第一个字符的从零开始的索引;如果没有找到子字符串或字符则返回-1。

  参数
  ch 要搜索的单个字符。
  lpszSub 要搜索的子字符串。
  nStart 字符串中开始搜索的字符的索引,如果是0,则是从头开始搜索。如果nStart不是0,则位于nStart之前的字符不包括在搜索之内。
  pstr 指向要搜索的字符串的指针

  说明
  此成员函数用来在此字符串中搜索子字符串的第一个匹配的字符。函数的重载可以接收单个字符(类似于运行时函数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',2); //结果 n = 2
第3个回答  2011-12-01
共有四种形式:
第一种:int Find(TCHAR ch ) const //从下标为0的位置开始查找,查找一个字符
第二种:int Find(LPCTSTR lpszSub) const //从下标为0的位置开始查找,查找一串字符
第三种:int Find(TCHAR ch,int nStart) const //从下标为nStart的位置开始查找,查找一个字符
第四种:int Find(LPCTSTR pstr,int nStart) const //从下标为nStart的位置开始查找,查找一串字符,其中pstr为指向要查找的字符串的指针

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网