C ++使用空格和引号对字符串进行标记 [英] C++ Tokenize a string with spaces and quotes
问题描述
我想用C ++编写一些标记字符串的东西.要解释我想要什么,请采用以下字符串:
I would like to write something in C++ that tokenize a string. To explain what I want, take the following string:
add string "this is a string with spaces!"
必须将其拆分为以下内容:
This must be splitted as follows:
add
string
this is a string with spaces!
是否有一种快速且基于标准库的方法?
Is there a quick and standard-library-based approach?
推荐答案
我想标准库没有直截了当的方法.间接遵循算法将起作用:
a)使用 string :: find('\')
搜索'\"'.如果找到任何内容,请使用 string :: find('\'',prevIndex)
搜索下一个'\';如果找到任何内容,请使用 string :: substr()
.部分来自原始字符串.
b)现在以相同的方式为''
字符搜索.
注意:您必须遍历整个字符串.
I guess there is no straight forward approach with standard library. Indirectly following algo will work:
a) search for '\"' with string::find('\"')
. If anything found search for next '\"' using string::find('\'',prevIndex)
, If found use string::substr()
. Discard that part from the original string.
b) Now Serach for ' '
character in the same way.
NOTE: you have to iterate through the whole string.
这篇关于C ++使用空格和引号对字符串进行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!