c语言中strtok函数
In this article, we’ll take a look at using the strtok() and strtok_r() functions in C.
在本文中,我们将介绍如何在C语言中使用strtok和strtok_r函数。
These functions are very useful, if you want to tokenize a string. C provides these handy utility functions to split our input string into tokens.
如果要标记字符串,这些功能非常有用。 C提供了这些方便的实用程序函数,可将我们的输入字符串拆分为标记。
Let’s take a look at using these functions, using suitable examples.
让我们使用合适的示例来看看如何使用这些功能。
使用strtok函数 (Using the strtok() function)
First, let’s look at the strtok() function.
首先,让我们看一下strtok函数。
This function is a part of the<string.h>
header file, so you must include it in your program.
此函数是<string.h>
头文件的一部分,因此必须在程序中包含它。
#include <string.h>char* strtok(char* str, const char* delim);
This takes in an input stringstr
and a delimiter characterdelim
.
这将接受输入字符串str
和定界符delim
。
strtok()
will split the string into tokens based on the delimited character.
strtok()
将根据定界字符将字符串拆分为标记。
We expect a list of strings fromstrtok()
. But the function returns us a single string! Why is this?
我们期望从strtok()
获得字符串列表。 但是该函数返回一个字符串! 为什么是这样?
The re