Header files: #include <string.h>
Define function: char * strtok (char *s, const char *delim);
Function Description: Strtok () is used to split a string into fragments. The parameter S points to the string to be split, the parameter Delim is the split string, and when Strtok () discovers the split character of the parameter Delim in the string of the parameter s, the character is changed to the \ = character. At the first call, Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. Each successful call returns the next segmented string pointer.
Return value: Returns the next segmented string pointer, or null if no partition has been made.
Example
1#include <string.h>2 Main () {3 CharS[] ="ab-cd:ef;gh:i-jkl;mnop;qrs-tu:vwx-y;z";4 Char*delim ="-: ";5 Char*p;6printf"%s", Strtok (S, Delim));7 while(P =strtok (NULL, Delim )))8printf"%s", p);9printf"\ n");Ten}
Execution Result:
AB CD ef;gh i jkl;mnop;qrs tu vwx y;z//-with: Character has been replaced by \ 0 character
Applications in the company's projects:
1 intCdevnetcomm::splitstring (Char Const* Strsource, STD::VECTOR<STD::string>& tokens,Char*szseps)2 {3 tokens.clear ();4 Charszsource[ the];5 strcpy (Szsource, strsource);6 Char* token = Strtok ((Char*) Szsource, szseps);7 while(Token! =NULL)8 {9 Tokens.push_back (token);Tentoken =strtok (NULL, szseps); One } A return(int) tokens.size (); -}
C language Strtok () function: String segmentation