C 中的字符串标记
我一直在尝试使用空格作为分隔符来标记字符串,但它不起作用。 有人对为什么它不起作用有建议吗?
编辑:标记化使用:
strtok(string, " ");
代码如下
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
I have been trying to tokenize a string using SPACE as delimiter but it doesn't work. Does any one have suggestion on why it doesn't work?
Edit: tokenizing using:
strtok(string, " ");
The code is like the following
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这样做:
注意:
strtok
修改字符串的标记,因此它不能是const char*
。Do it like this:
Note:
strtok
modifies the string its tokenising, so it cannot be aconst char*
.以下是
strtok
用法的示例,请记住strtok
会破坏其输入字符串(因此永远不能用于字符串常量基本上需要注意的是,将
NULL
作为第一个参数传递给strtok
会告诉它从之前标记的字符串中获取下一个标记。Here's an example of
strtok
usage, keep in mind thatstrtok
is destructive of its input string (and therefore can't ever be used on a string constantBasically the thing to note is that passing a
NULL
as the first parameter tostrtok
tells it to get the next token from the string it was previously tokenizing.strtok 可能非常危险。 它不是线程安全的。 它的预期用途是在循环中一遍又一遍地调用,并传入上一次调用的输出。 strtok 函数有一个内部变量,用于存储 strtok 调用的状态。 这种状态并不是每个线程独有的——它是全局的。 如果任何其他代码在另一个线程中使用 strtok,则会出现问题。 也不是您想要追踪的那种问题!
我建议寻找正则表达式实现,或使用 sscanf 来分解字符串。
试试这个:
注意:“文本”字符串在分离时被破坏。 这可能不是首选行为 =)
strtok can be very dangerous. It is not thread safe. Its intended use is to be called over and over in a loop, passing in the output from the previous call. The strtok function has an internal variable that stores the state of the strtok call. This state is not unique to each thread - it is global. If any other code uses strtok in another thread, you get problems. Not the kind of problems you want to track down either!
I'd recommend looking for a regex implementation, or using sscanf to pull apart the string.
Try this:
Note: The 'text' string is destroyed as it's separated. This may not be the preferred behaviour =)
您可以通过引入额外的变量来简化代码。
You can simplify the code by introducing an extra variable.
我制作了一些字符串函数来分割值,尽可能使用更少的指针,因为此代码旨在在 PIC18F 处理器上运行。 当可用的空闲 RAM 很少时,这些处理器不能很好地处理指针:
I've made some string functions in order to split values, by using less pointers as I could because this code is intended to run on PIC18F processors. Those processors does not handle really good with pointers when you have few free RAM available:
在阅读 strtok 文档时,我发现您需要在第一次“初始化”调用后传入 NULL 指针。 也许你没有这样做。 当然只是猜测。
When reading the strtok documentation, I see you need to pass in a NULL pointer after the first "initializing" call. Maybe you didn't do that. Just a guess of course.
这是另一个
strtok()
实现,它具有识别连续分隔符的能力(标准库的strtok()
没有这个功能)该函数是 BSD 许可字符串的一部分库,名为 zString。 非常欢迎您做出贡献:)
https://github.com/fnoyanisi/zString
如中所述之前的帖子,由于
strtok()
或我上面实现的那个,依赖于static *char
变量来保留连续调用之间最后一个分隔符的位置,因此应该格外小心在处理多线程应用程序时采取。Here is another
strtok()
implementation, which has the ability to recognize consecutive delimiters (standard library'sstrtok()
does not have this)The function is a part of BSD licensed string library, called zString. You are more than welcome to contribute :)
https://github.com/fnoyanisi/zString
As mentioned in previous posts, since
strtok()
, or the one I implmented above, relies on astatic *char
variable to preserve the location of last delimiter between consecutive calls, extra care should be taken while dealing with multi-threaded aplications.您
可以在我的个人资料中提到的博客中阅读详细分析:)
}
You can read detail analysis at blog mentioned in my profile :)