Home>Article>Backend Development> What is the usage of strtok function?

What is the usage of strtok function?

coldplay.xixi
coldplay.xixi Original
2020-10-28 09:48:27 16324browse

Usage of strtok function: 1. Acts on the string s, using the characters in delim as delimiters to split s into a substring; 2. If s is NULL, the pointer saved by the function [SAVE_PTR] will be used as the starting position in the next call.

What is the usage of strtok function?

##Related free recommendations:Programming video course

Usage of strtok function:

1. Introduction to the function

Function prototype: char *strtok(char *s, char *delim)

Function: Acts on the string s, using the characters in delim as delimiters to divide s into a substring; if s is NULL, the pointer SAVE_PTR saved by the function is as follows Will be used as the starting position in a call.

Return value: The first substring matched by the delimiter

2. Main content

Decompose a string. The so-called decomposition means that no new string is generated, but the delimiter is changed to '/0' at the position where the delimiter first appears in the content pointed to by s. Therefore, strtok() is used for the first time to return the first Substring

## 2.
After the first substring extraction is completed, continue to extract the source string s, which should be followed (the second time, the third time. ..In the nth call), assign the first parameter of strtok to the null value NULL (

indicates that the function continues to decompose the string from the position implicitly saved in the previous call; for the previous calls , before the first call ends, use a this pointer to point to the next digit of the delimiter

)3.

When this pointer points to "\0", That is, there are no divided substrings. At this time, NULL is returned.

4.

can be understood as delim The characters in the setanddelim can be used as separators.

5. Whenstrtok is called, if the starting position is the delimiter, the starting position is ignored The starting delimiter

##3.
Things to note when using strtok There are the following points:

1. The function of the function is to decompose the string. The so-called decomposition means that no new string is generated, but only some manipulation is done on the content pointed to by s. Therefore, the source string s has changed!Suppose the source string s is char buffer[INFO_MAX_SZ]=",Fred male 25,John male 62,Anna female 16"; The filter string delim is char *delim = " ", that is, the space is the delimiter .

The code in the above picture will produce this result:

What is the usage of strtok function?

First,

buffer has changedWhat is the usage of strtok function?. If the buffer value is printed at this time, ",Fred" will be displayed, and the following "male 25...16" will disappear. In fact, the strtok function finds the position where it first appears based on the delimiter in delim, that is, the space after Fred (buffer[5]), and changes it to '/0'. The remaining positions remain unchanged. This explains why the value of the printed buffer can only appear as ",Fred" instead of the entire contents of the buffer.

Therefore, be careful when using strtok to prevent the source string from being modified.

After understanding the changes in buffer, it is easy to explain the return value of the function. The return value buf is the substring before the delimiter (in fact, this statement is not accurate, please see the detailed description of the return value in "3").Note that from the address of the variable, buf still points to the source string.

#The delimiter delim has not changed, so no more screenshots will be taken.

What is the usage of strtok function?

2. If you want to continue to extract the source string s after the first extraction of the substring, you should add the following (second, third . . . The nth) call assigns the first parameter of strtok to the null value NULL.

The result of the first call is as mentioned above, ",Fred" is extracted. We also want to continue to use spaces as boundaries to extract the following "male" and so on. As can be seen from the above figure, for the first call after the first call, we passed the null value NULL to the first parameter of strtok (means that the function continues to decompose the string from the position implicitly saved in the previous call; for For the second call above, before the end of the first call, a this pointer is used to point to the next bit of the delimiter, that is, the position of 'm'), so that

## can be extracted in sequence.

#,What is the usage of strtok function?. . . . And so on. . . . .What is the usage of strtok function?

As for why you need to assign a null value, you can either remember the conclusion or check the source code of strtok. There will be some introduction at the end of this article.

Of course, there are also some people who love to get into trouble. They have to play their cards according to the routine and see what the consequences will be if they continue to assign the value to the buffer without assigning a null value. In fact, the answer can be imagined. Passing the buffer again is equivalent to searching for the delimiter delim from the beginning of the string, and the buffer has been modified at this time (only ",Fred" is left in the visible part). Therefore, the result must be that the delimiter delim cannot be found. .

3. Discussion on function return value

As mentioned in "1",

When a substring is extracted, the return value of strtok (Assuming that the return value is assigned to the pointer buf) is the pointer to the extracted substring. This pointer points to the starting position of the substring in the source string. The next character at the end of the substring is the delimiter before extraction, but is modified to '/0' after extraction.Therefore, if the value of buf is printed, the content of the substring can be successfully output.

What value will the function return if the substring is not extracted?

What is the usage of strtok function?

#As you can see from the above figure, the buffer does not contain the delimiter delim. After calling strtok, the value of buf is

What is the usage of strtok function?##. Because it is not found, the source string buffer has not changed. buf points to the first address of the source string, and the printed output value is the entire character. The complete value of the string.

When does the return value of a function be NULL?

Baidu Encyclopedia says, "

When there is no split string, NULL is returned.

" This is a very ambiguous statement. If you want to understand this issue clearly, you may need to take a look at the implementation principle of strtok. Here is an experimental explanation first.

What is the usage of strtok function?The first time strtok is called, there is no doubt that buf points to ",Fred".

The second time strtok is called, since the first parameter is NULL, it means that the function continues to decompose from the position of this pointer saved in the last call, that is, decompose "male 25". After decomposition is completed, buf points to "male".

The third time strtok is called, the parameter continues to be set to NULL. At this time, the position of this pointer saved for the second time begins to be decomposed, that is, "25" is decomposed. Because the substring containing the delimiter delim cannot be found, buf points to "25".

What is the usage of strtok function?The fourth call, the parameter is still NULL. At this time, the this pointer saved in the third call has pointed to the end of the string '/0', and no further progress can be made. break down. Therefore, the function returns NULL, which is what is mentioned in Baidu Encyclopedia: "The function returns NULL when there is no divided string."

What is the usage of strtok function?

4 .Discussion on parameter delimiter delim (delim is a set of delimiters)

When many people use strtok, they take it for granted that the function completely matches the delimiter delim when splitting a string, such as delim="ab ", then for the string "acdab", the function extracts "acd". At least that's what I thought when I first used it. In fact, we were all wrong. I only discovered this problem when I looked at the source code of the function. Let's look at the following example.

What is the usage of strtok function?The source string is buffer, and the delimiter delim is comma and space. According to the general idea, we would think that after calling the function, the value of buf is "Fred,male,25 ", is this the result?

What is the usage of strtok function?

#The result after the first call turned out to be "Fred", not the result we thought.Why is this?

We return to the function definition of strtok in the GNU C Library: "Parse S into tokens separated by characters in DELIM".That is to say, the characters included in delim can be used as separators instead of strict matching. Delim can be understood as a collection of delimiters. This point is very important~

Of course, when we decompose a string, we rarely use multiple delimiters. This also leads to many people only discussing the case of one separator when writing examples. More people misunderstand the role of delim when looking at examples.

5. The first character of the string to be decomposed is the delimiter.

It is not a special case that the first character is the delimiter. Strings can also be decomposed correctly according to conventional decomposition ideas.

What I want to explain is that strtok adopts a faster than normal processing method for this situation.

What is the usage of strtok function?

As shown in the example above. The comma-separated string "Fred male 25" can be obtained with just one call, and the ',' in front of the F is ignored. It can be seen thatstrtok ignores the delimiter starting from the starting position when calling.This can be confirmed from the source code of strtok.

6. You cannot pass a string constant to the first parameter!

The examples given in this article all save the source string as a string array variable. If you define the source string as a string constant, you can imagine that the program will throw an exception because the strtok function attempts to modify the value of the source string.

What is the usage of strtok function?

The above is the detailed content of What is the usage of strtok function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn