字符串分隔函數strtok

strtok  

原型:char  *strtok( char *str,  char *delim );
  

功能: 分解字符串爲一組字符串。s爲要分解的字符串,delim爲分隔符字符串。

           根據delimit中的字符來分割字符串 str。

 

說明: 首次調用時,s指向要分解的字符串的首地址,之後再次調用要把s設成NULL。

          strtok在s中查找包含在delim中的字符並用NULL('')來替換,直到找遍整個字符串。

          所有delim中包含的字符都會被濾掉,並將被濾掉的地方設爲一處分割的節點。

          第一次調用strok時,自動去除掉最前面的分隔符.


返回值:從s開頭開始的一個個被分割的字符串。字符串不可再分時,即查找不到新的token時(沒有被分割的串時)則返回
NULL,否則返回分隔符前的字符串指針.


注意!: 如果當其遇到“/0”(字符串結束符),再調用strtok將返回NULL。

  
例子:
#include<stdio.h>
#include   <string.h>
main() {
   char a[50]="4567898521234951357654",delim[2]="5";
   char *p;
   p=strtok(a, delim);
   while( p ) {
       printf("%s、n", p);
       p=strtok(NULL, delim);
   }
}

輸出結果:

4

67898

212349

13

76

4

 

數組a中存儲值得變化: 

第一次循環:4 67898521234951357654

第二次循環:4 67898 21234951357654

第三次循環:4 67898 212349 1357654

第四次循環:4 67898 212349 13 7654

第五次循環:4 67898 212349 13 76 4

 

其他的一些方法:1、Split 函數:返回值數組 = Split("字符串","分割符")
                              2、CString   Find,然後截取

 

附錄:

以下是msdn中的解釋:

函數原型:

char *strtok_s( char *strToken, const char *strDelimit, char **context);

char *_strtok_s_l( char *strToken, const char *strDelimit, char **context, _locale_t locale);

wchar_t *wcstok_s( wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context);

wchar_t *_wcstok_s_l( wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context, _locale_t locale);

unsigned char *_mbstok_s( unsigned char*strToken, const unsigned char *strDelimit, char **context);

unsigned char *_mbstok_s( unsigned char*strToken, const unsigned char *strDelimit, char **context, _locale_t locale);

參數:

strToken  String containing token or tokens.

strDelimit  Set of delimiter characters.

context  Used to store position information between calls tostrtok_s

locale  Locale to use.
Returns a pointer to the next token found instrToken. They returnNULL when no more tokens are found. Each call modifies strTokenby substituting aNULL character for each delimiter that is encountered.
 

char *strtok_s( char *strToken, const char *strDelimit, char **context);

例子:
#include<stdio.h>
#include   <string.h>
main() {
       WCHAR *pszResult = NULL;
       WCHAR str[] = L"asd2fghj2kl";
       WCHAR *subStr;
       subStr = wcstok_s(str, _T("2"), &pszResult);
       while (subStr) {
                    printf("%S\n", subStr);
                    subStr = wcstok_s(NULL, _T("2"), &pszResult);
       }
}

輸出結果:      pszResult值:

asd                    fghj2kl

fghj                    kl

kl                       (“”)

 

以下是老版本的msdn中的解釋:

strtok, wcstok, _mbstok

Find the next token in a string.

char  *strtok( char *strToken,  const char *strDelimit);

wchar_t  *wcstok( wchar_t *strToken,  const wchar_t *strDelimit);

unsigned char *_mbstok( unsigned char*strToken,  const unsigned char *strDelimit);

 

Routine Required Header Compatibility
strtok <string.h> ANSI, Win 95, Win NT
wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbstok <mbstring.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

All of these functions return a pointer to the next token found in strToken. They returnNULL when no more tokens are found. Each call modifiesstrToken by substituting a NULL character for each delimiter that is encountered.

Parameters

strToken

String containing token(s)

strDelimit

Set of delimiter characters

Remarks

The strtok function finds the next token in strToken. The set of characters instrDelimit specifies possible delimiters of the token to be found instrToken on the current call.wcstok and_mbstok are wide-character and multibyte-character versions ofstrtok. The arguments and return value ofwcstok are wide-character strings; those of_mbstok are multibyte-character strings. These three functions behave identically otherwise.

 


LPTSTR GetBuffer(int nMinBufLength)
      這個函數是爲一個CString對象重新獲取其內部字符緩衝區的指針,返回的LPTSTR爲非const的,從而允許直接修改CString中的內容。
      GetBuffer(int size)是用來返回一個你所指定大小可寫內存的成員方法。

      它和被重載的操作符LPCTSTR還是有點本質區別的,LPCTSTR是直接返回一個只讀內存的指針,

       而GetBuffer則是返回一個可以供調用者寫入的內存,並且,你可以給定大小。
      GetBuffer()的參數是你需要CString爲緩衝區分配的最小長度。如果因爲某
些原因,你需要一個可修改的緩衝區來存放1K TCHARs,你需要調GetBuffer(1024)。

      把0作爲參數時,GetBuffer()返回的是指向字符串當前內容的指針。

 
ReleaseBuffer
      GetBuffer暴露pData讓用戶輸入;ReleaseBuffer做些掃尾工作,如設置
Length等。

      如果你使用這個指向由GetBuffer所改變返回的字符串內容,那麼在你使用CString其他CString方法之前你必須調用ReleaseBuffer。

原型:void ReleaseBuffer( int nNewLength = -1 );

參數:nNewLength

     The new length of the string in characters, not counting a null terminator. If the string is null-terminated, the -1 default value sets theCString size to the current length of the string.


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章