MFC的CString的find的使用

2021-07-26 17:49:34 字數 1059 閱讀 4371

名稱

cstring

::find

在乙個較大的

字元 串中查詢字元或子字串

int find( tchar ch ) const;

int find( lpctstr lpszsub ) const;

int find( tchar ch, int nstart ) const;

int find( lpctstr pstr, 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',5);         //結果 n = 12

MFC的CString的內部實現分析

mfc的cstring是字串管理類,其為了實現高效率的緩衝管理,使用了引用記數及copybeforewrite技術。這在一定程度上加大了其神秘感和理解難度。好在他的 是公開的,所以可以給我們研究它的內部實現提供條件。下面就來看看到底是如何實現的。由於不同版本的msvc其cstring實現有些許差別,...

MFC之CString物件的各種函式

一 mfc常用類之 string 類 1 cstring的建構函式 cstring const cstring stringsrc 將乙個已經存在的cstring 物件 stringsrc 的內容拷貝到該 cstring 物件。例如 cstring str1 t www.google.com 將常量...

關於MFC的CString 訪問越界問題

1.很多控制項可能都要使用cstring,比如ctooltipctrl.gettext 如果我一開始,m tooltip.addtool this,m str,rect,tts alwaystip 其中 m str 的長度超過256位元組,注意是位元組,如果是unicode下,那就128 tchar...