字符串标记/解析 [英] String tokens/parsing

查看:93
本文介绍了字符串标记/解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(如果这是常见问题解答,我为没有找到它而道歉)


我有一个C风格的字符串,我想干净地分成标记

(基于''。''字符)然后将这些标记转换为无符号

整数。什么是最好的标准(!)C ++方法来实现这个目标?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

(if this is a FAQ, I apologize for not finding it)

I have a C-style string that I''d like to cleanly separate into tokens
(based on the ''.'' character) and then convert those tokens to unsigned
integers. What is the best standard(!) C++ way to accomplish this?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
(如果这是常见问题解答,我为没有找到而道歉it)

我有一个C风格的字符串,我想干净地分成标记
(基于''。''字符),然后将这些标记转换为无符号
整数。什么是最好的标准(!)C ++方法来实现这个目标?
(if this is a FAQ, I apologize for not finding it)

I have a C-style string that I''d like to cleanly separate into tokens
(based on the ''.'' character) and then convert those tokens to unsigned
integers. What is the best standard(!) C++ way to accomplish this?




strtok函数会在字符串中找到令牌并且

修改你的字符串。


或许strchr找到''。''。


另一个函数是sscanf。我听说你可以设置

格式描述符字符串,以便正确解析。

{这可能是一项艰巨的任务)。我敢肯定,如果你把新闻发布到新闻:comp.lang.c,Dan Pop将会显示出来。


至于C ++,你可以想要转换为std :: string

并使用find方法,也许是字符串流

转换为int。


-

Thomas Matthews

C ++新闻组欢迎辞:
http:/ /www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍



The strtok function will find tokens in the string and
modify your string.

Perhaps strchr to find the ''.''.

Another function is sscanf. I''ve heard that you can set
the format descriptor string so that it parses correctly.
{which may be a difficult task). I''m sure if you post
to news:comp.lang.c, Dan Pop will show the way.

As for C++, you may want to convert to a std::string
and use the "find" methods and maybe a stringstream
for converting to an int.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Thomas Matthews< Th ******************* *********@sbcglobal.net>这样说:
Thomas Matthews <Th****************************@sbcglobal.net> spoke thus:
strtok函数会在字符串中找到标记并修改你的字符串。
也许strchr找到''。''
另一个函数是sscanf。我听说你可以设置
格式描述符字符串,以便正确解析。
{这可能是一项艰巨的任务)。我敢肯定,如果你发布新闻:comp.lang.c,Dan Pop会表明道路。
The strtok function will find tokens in the string and
modify your string. Perhaps strchr to find the ''.'' Another function is sscanf. I''ve heard that you can set
the format descriptor string so that it parses correctly.
{which may be a difficult task). I''m sure if you post
to news:comp.lang.c, Dan Pop will show the way.




相信我,我''我完全有能力用C做这件事,并且我没有

comp.lang.c的陌生人。我在这里专门发布是因为我很有兴趣改进C方法,如果实际上是可行的话。

原始的C代码(我们被卡住了)在一个C-style-C ++范例中,不幸的是
)让我感到非常黑客。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Believe me, I''m perfectly capable of doing this with C, and am no
stranger to comp.lang.c. I posted here specifically because I''m
interested in improving on the C methods, if that is in fact possible.
The original C code (we''re stuck in a "C-style-C++" paradigm,
unfortunately) strikes me as being distinctively hack-y.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
相信我,我完全有能力用C做这件事,对comp.lang.c不是很陌生。我在这里专门发布是因为我对改进C方法很感兴趣,如果事实上可行的话。
最初的C代码(我们被困在C风格 - 不幸的是,C ++paradigm,
让我感到非常黑客。
Believe me, I''m perfectly capable of doing this with C, and am no
stranger to comp.lang.c. I posted here specifically because I''m
interested in improving on the C methods, if that is in fact possible.
The original C code (we''re stuck in a "C-style-C++" paradigm,
unfortunately) strikes me as being distinctively hack-y.



那么你需要更具体地了解你已经拥有的东西,以及

您的要求是。您是否反对将C风格的字符串

转换为std :: strings或std :: stringstreams?你有什么限制?
" C-style-C ++"范式强加?你在多大程度上可以偏离C

标准库?


你有一个非常模糊的问题。


Brian Rodenborn


Then you need to be more specific about what you already have, and what
your requirements are. Do you object to converting the C-style strings
to std::strings or std::stringstreams? What limitations does your
"C-style-C++" paradigm impose? To what extent can you deviate from the C
standard library?

You have a very vague question.


Brian Rodenborn


这篇关于字符串标记/解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆