Unlike advanced languages such as Java and PHP, C language directly encapsulates string processing functions in objects. Common string processing in the C language often causes us to be overwhelmed ...... Fortunately, the C language provides powerful string processing functions such as strtok, which can help us implement some of the required functions. Next we will introduce the usage of strtok functions and specific examples.
Original Type: char * strtok (char * s, char * delim );
Function: break down a string into a group of strings. S is the string to be decomposed, and delim is the separator string. Essentially, strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Description: During the first call, s points to the string to be decomposed, and then calls it again to set S to null. Strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Return Value: Split strings starting with S. If no split string exists, null is returned. All delim characters are filtered out, And the filtered characters are set as a separated node.
Example:
Download: strtok_sample.c
-
- # Include<Stdio. h>
-
- # Include<String. h>
- IntMain(IntArgc,Char**Argv)
-
- {
-
- Char*Buf1="Aaa, A, BBB-C, ee | ABC";
-
- /* Establish string and get the first token :*/
- Char*Token=Strtok( Buf1,",-|");
-
- While( Token! =Null)
-
- {
-
- /* While there are tokens in "string "*/
- Printf( "% S",Token );
-
- /* Get next token :*/
-
- Token=Strtok(Null,",-|");
- }
-
- Return 0;
-
- }
From: http://blog.minidx.com/2008/02/03/469.html