天天看點

常用字元串函數

常用字元串函數

1. 字元串比較函數

//比較兩個字元串是否相同  int StrCmp(LPCTSTR  lpStr1 ,LPCTSTR  lpStr2 ); int StrCmpN(LPCTSTR  lpStr1 ,LPCTSTR  lpStr2 ,int  nChar ); int strcmp( const char * string1, const char * string2 ); int wcscmp( const wchar_t * string1, const wchar_t * string2 ); int CompareString( LCID Locale DWORD dwCmpFlags LPCTSTR lpString1 int cchCount1 LPCTSTR lpString2 int cchCount2 );

2. 計算字元串長度            HRESULT StringCchLength( LPCTSTR  psz ,size_t  cchMax ,size_t * pcch ); //replacement for strlen           size_t strlen( const char * string );

          size_t wcslen( const wchar_t * string );   3. 字元串指派函數            HRESULT StringCchCopy(LPTSTR  pszDest ,size_t  cchDest ,LPCTSTR  pszSrc ); //replacement for strcpy         

HRESULT StringCchCopyN( LPTSTR 

pszDest

, size_t 

cchDest

, LPCTSTR 

pszSrc

, size_t 

cchSrc ); //replacement for strncpy          LPTSTR StrCpy(LPTSTR  psz1 ,LPCTSTR  psz2 );  //存在安全問題           LPTSTR StrCpyN(LPTSTR  psz1 ,LPCTSTR  psz2 ,int  cchMax );  //存在安全問題           char *strcpy( char * strDestination, const char * strSource );

          wchar_t *wcscpy( wchar_t * strDestination, const wchar_t * strSource );           char *strncpy( char * strDest , const char * strSource , size_t count );

          wchar_t *wcsncpy( wchar_t* strDest , const wchar_t * strSource , size_t count );   4. 字元串連接配接函數          HRESULT StringCchCat( LPTSTR  pszDest ,size_t  cchDest ,LPCTSTR  pszSrc ); //replacement for strcat          HRESULT StringCchCatN( LPTSTR  pszDest ,size_t  cchDest ,LPCTSTR  pszSrc ,size_t  cchMaxAppend ); //replacement for strncat         LPTSTR StrCat( LPTSTR  psz1 ,LPCTSTR  psz2 );  //存在安全問題          LPTSTR StrNCat( LPTSTR  pszFront ,LPCTSTR  pszBack ,int  cchMax );  //存在安全問題        

char *strcat( char

* strDestination,

const char

* strSource

);          wchar_t *wcscat( wchar_t

* strDestination,

const wchar_t

* strSource

);

         char *strncat( char * strDest, const char * strSource, size_t count );

         wchar_t *wcsncat( wchar_t * strDest, const wchar_t * strSource, size_t count );   5. 字元查找函數            //查找字元串中指定字元第一次出現的位置          LPTSTR StrChr( LPCTSTR  lpStart ,TCHAR  wMatch ); //區分大小寫          char *strchr( const char * string, int c );

         wchar_t * wcschr( const wchar_t * string, wchar_t c );          LPTSTR StrChrI( LPCTSTR  lpStart ,TCHAR  wMatch ); //不區分大小寫          //查找字元串中指定字元最後一次出現的位置        LPTSTR StrRChr( LPCTSTR  lpStart ,LPCTSTR  lpEnd ,TCHAR  wMatch ); //區分大小寫       

char *strrchr( const char

* string

, int

c

);          wchar *wcsrchr( const wchar_t

* string,

int

c

);

         LPTSTR StrRChrI( LPCTSTR  lpStart ,LPCTSTR  lpEnd ,TCHAR  wMatch ); //不區分大小寫                *注   StrRChr()函數可以通過StrChr()函數和while循環來實作。   補充: 查找字元串:

    _tcsstr(........)

字元串轉化為double型數字

    _tcstod( const char *nptr,   char **endptr )

字元串轉化為double型整數(隻取整數部分,不取小數)Convert strings to a long-integer value.

     _tcstoul  (  const char *nptr,   char **endptr,   int base )

    _tcstol  (  const char *nptr,   char **endptr,   int base )

取子字元串

Extracts a substring of length nCount characters from this

CStringT

object, starting at position iFirst (zero-based).

CStringT Mid(
   int iFirst,
   int nCount
) const;
CStringT Mid(
   int iFirst
) const;      
example:      
//typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;

CAtlString s( _T("abcdef") );
_ASSERT( s.Mid( 2, 3 ) == _T("cde") );