1. 程式人生 > >CString字串查詢和擷取

CString字串查詢和擷取

1、Find() 該函式從左側0索引開始,查詢第一個出現的字元位置 CString str( “abc” ); int postion=str.Find(‘a’); 如果查到,返回以0索引起始的位置;未查到,返回-1。 2、FindOneOf() 給定一字串,然後查詢其中出現的第一個字元位置 CString str( “abc” ); int position=str.FindOneOf(“ab”); 如果查到,返回以0索引起始的位置;未查到,返回-1。 3、ReverseFind() 該函式反向查詢字元出現的位置。示例如下: CString str( “abc” ); int position=str.ReverseFind(‘a’); 如果查到,返回以0索引起始的位置;未查到,返回-1。

二、CString之Left()、Mid()、Right() CString有如下字串擷取函式: 1、Left(int nCount) 該函式擷取左側nCount個字元,如果遇到雙位元組字元比如中文,則可能會截斷亂碼,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個位元組字元 s=“abcdefghijk”; s1=s.Left(2); //ab s2=s.Mid(3); //defghijk s4=s.Right(3); //ijk