在C++中解析(拆分)字符串使用字符串定界符(标准C++)
我使用以下内容在C ++中解析一个字符串:
using namespace std;
string parsed,input="text to be parsed";
stringstream input_stringstream(input);
if (getline(input_stringstream,parsed,' '))
{
// do some processing.
}
用单个CHAR定界符解析是可以的。但是,如果我想将字符串用作定界符怎么办。
示例:我想拆分:
scott>=tiger
用> =
作为定界符,以便我可以得到Scott和Tiger。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我做这个解决方案。它非常简单,所有打印/值都在循环中(循环后不需要检查)。
感谢@steveward改善了这个答案。
I make this solution. It is very simple, all the prints/values are in the loop (no need to check after the loop).
Thanks to @SteveWard for improving this answer.
这是一种完整的方法,可以将字符串在任何定界符上拆分并返回切碎的字符串的向量。
这是Ryanbwork的答案的改编。但是,他的检查:
如果(token!= mystring)
如果字符串中有重复元素,则会给出错误的结果。这是我解决这个问题的方法。This is a complete method that splits the string on any delimiter and returns a vector of the chopped up strings.
It is an adaptation from the answer from ryanbwork. However, his check for:
if(token != mystring)
gives wrong results if you have repeating elements in your string. This is my solution to that problem.由于这是
c ++ split String
或类似的顶级堆栈溢出的Google搜索结果,因此我将发布一个完整的,复制/粘贴可运行的示例,显示这两种方法。SplitString
使用stringstream
(在大多数情况下可能是更好,更容易的选项)splitstring2
use查找
和subSttr
(一种更多的手动方法)Since this is the top-rated Stack Overflow Google search result for
C++ split string
or similar, I'll post a complete, copy/paste runnable example that shows both methods.splitString
usesstringstream
(probably the better and easier option in most cases)splitString2
usesfind
andsubstr
(a more manual approach)另一个答案:在这里,我正在使用
find_first_not_of
字符串函数返回 not 不匹配的第一个字符的位置匹配指定的任何字符在特征中。示例:
输出:
Yet another answer: Here I'm using
find_first_not_of
string function which returns the position of the first character that does not match any of the characters specified in the delim.Example:
Output:
如果您不想修改字符串(如Vincenzo pii的答案中)和也希望输出最后一个令牌,则可能需要使用此方法:
If you do not want to modify the string (as in the answer by Vincenzo Pii) and want to output the last token as well, you may want to use this approach:
这是一个简洁的拆分功能。我决定让背对背的定界器作为一个空字符串返回,但是您可以轻松检查子字符串是否为空,并且如果是空字符串,则不会将其添加到向量。
Here's a concise split function. I decided to have back to back delimiters return as an empty string but you could easily check that if the substring is empty and not add it to the vector if it is.
此方法使用 string find 和 string substr
This method use string find and string substr
PS:仅当分裂后的字符串长度相等时才起作用
P.S: Works only if the lengths of the strings after splitting are equal
功能:
单位测试:
Function:
Unit-tests:
我使用指针算术。内部为字符串区别仪,如果您与char系列满足,只需简单地删除内部即可。我希望这是正确的。如果您发现任何错误或改进,请发表评论。
i use pointer arithmetic. inner while for string delimeter if you satify with char delim just remove inner while simply. i hope it is correct. if you notice any mistake or improve please leave the comment.
一个简单的解决方案是 -
您可以使用
strtok
根据多级定界符进行界定。请记住使用
strdup
,以使Orignal String不会突变。A simpler solution would be -
You can use
strtok
to delimit on the basis of multichar delimiter.Remember to use
strdup
so that the orignal string isn't mutated.我浏览了答案,但还没有看到一个基于迭代的方法,该方法可以馈入范围循环,所以我做了一个。
这使用C ++ 17 String_views,因此不应分配字符串的副本。
示例用法将是:
哪个打印:
它在字符串的开头和结尾处正确处理空条目。
I looked through the answers and haven't seen an iterator based approach that can be fed into a range loop, so I made one.
This uses C++17 string_views so it shouldn't allocate copies of the string.
And example usage would be:
Which prints:
It properly handles empty entries at the beginning and end of the string.
这是使用库和 boost范围库。该解决方案的灵感来自stringalgo库文档中的(谦虚)建议,请参阅 split 部分。
以下是一个完整的程序,具有
split_with_string
函数以及综合测试 - 与godbolt尝试:测试输出:
Here is an example of splitting a string with another string using Boost String Algorithms library and Boost Range library. The solution is inspired with (modest) suggestion from the the StringAlgo library documentation, see the Split section.
Below is a complete program with the
split_with_string
function as well as comprehensive test - try it with godbolt:Tests output:
您可以使用
std :: string :: string :: find()
功能要找到字符串定界符的位置,然后使用std :: string :: substr()
获得一个令牌。示例:
查找(const String&str,size_t pos = 0)
函数返回字符串中str
的首次出现的位置=“ http://en.cppreference.com/w/cpp/string/basic_string/npos” rel =“ noreferrer”>npos
如果找不到字符串。substr(size_t pos = 0,size_t n = npos)
函数返回对象的子字符串,从位置开始pos
和长度npos < /代码>。
如果您有多个定界符,则提取一个令牌后,可以将其删除(包括定界符)继续进行后续提取(如果要保留原始字符串,只需使用
s = s.substr(pos + deleimiter) .length());
):这样,您可以轻松地循环获取每个令牌。
完整的示例
You can use the
std::string::find()
function to find the position of your string delimiter, then usestd::string::substr()
to get a token.Example:
The
find(const string& str, size_t pos = 0)
function returns the position of the first occurrence ofstr
in the string, ornpos
if the string is not found.The
substr(size_t pos = 0, size_t n = npos)
function returns a substring of the object, starting at positionpos
and of lengthnpos
.If you have multiple delimiters, after you have extracted one token, you can remove it (delimiter included) to proceed with subsequent extractions (if you want to preserve the original string, just use
s = s.substr(pos + delimiter.length());
):This way you can easily loop to get each token.
Complete Example
对于字符串定界符
基于A 字符串定界符的拆分字符串。例如,划分字符串
“ adsf-+qwret-+nvfkbdsj--+orthdfjgh--+dfjrleih”
基于字符串分隔符 code>“ - +” ,输出将为> {
adsf“,” qwret,“ nvfkbdsj”,“ orthdfjgh”,“ dfjrleih”}**Output**
对于单个字符定界符
基于字符定界符的拆分字符串。例如,将字符串
拆分为“ adsf+qwer+poui+fdgh”
with DeLimiter“+”
将输出{“ adsf”,“ qwer”,“ qwer”,“ poui”,“ poui” ,“ fdgh”}
**Output**
For string delimiter
Split string based on a string delimiter. Such as splitting string
"adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih"
based on string delimiter"-+"
, output will be{"adsf", "qwret", "nvfkbdsj", "orthdfjgh", "dfjrleih"}
**Output**
For single character delimiter
Split string based on a character delimiter. For example, splitting string
"adsf+qwer+poui+fdgh"
with delimiter"+"
will output{"adsf", "qwer", "poui", "fdgh"}
**Output**
此方法使用
std :: String :: find
在不突变原始字符串的情况下通过记住上一个子字符串令牌的开始和结尾。This method uses
std::string::find
without mutating the original string by remembering the beginning and end of the previous substring token.您可以使用下一个函数拆分字符串:
You can use next function to split string:
使用C ++ 20的一种方法:
请参阅:
https://stackoverflow.com/a/48403210/10771848
https://en.cppreference.com/w/cpp/ranges/split_view
A way of doing it with C++20:
See:
https://stackoverflow.com/a/48403210/10771848
https://en.cppreference.com/w/cpp/ranges/split_view
您还可以为此使用Regex:
等效于:
并像这样使用:
在线玩!
您可以简单地使用subsin,字符等,例如正常等,或使用实际的正则表达式进行分裂。
它也简洁,C ++ 11!
You can also use regex for this:
which is equivalent to :
and use it like this :
play with it online!
you can simply use substrings, characters, etc like normal, or use actual regular expressions to do the splitting.
its also concise and C++11!
答案已经存在,但是选定的答案使用擦除功能,这是非常昂贵的,想想一些非常大的字符串(在MBS中)。因此,我使用以下功能。
Answer is already there, but selected-answer uses erase function which is very costly, think of some very big string(in MBs). Therefore I use below function.
该代码将线条从文本分配,然后将所有人添加到向量中。
称为:
This code splits lines from text, and add everyone into a vector.
Called by:
strtok 允许您以多个字符作为定界器传递。我敢打赌,如果您通过“&gt; =”您的示例字符串将正确拆分(即使&gt; and =被计为单个定界符)。
编辑如果您不想使用
c_str()
将从字符串转换为char*,则可以使用 substr 和 tokenize。strtok allows you to pass in multiple chars as delimiters. I bet if you passed in ">=" your example string would be split correctly (even though the > and = are counted as individual delimiters).
EDIT if you don't want to use
c_str()
to convert from string to char*, you can use substr and find_first_of to tokenize.一种非常简单/幼稚的方法:
或者您可以使用boost库拆分函数:
或者您可以尝试代币或strtok:
或者您可以做到这一点:
A very simple/naive approach:
Or you can use boost library split function:
Or You can try TOKEN or strtok:
Or You can do this:
我将使用
boost :: tokenizer
。这里的文档说明了如何制作适当的引物函数:这是适合您的情况的一种。
I would use
boost::tokenizer
. Here's documentation explaining how to make an appropriate tokenizer function: http://www.boost.org/doc/libs/1_52_0/libs/tokenizer/tokenizerfunction.htmHere's one that works for your case.
这应该适用于字符串(或单个字符)定界符。不要忘记包括
#include&lt; sstream&gt;
。第一个使用字符串定界符的第一个字符提取循环提取令牌。第二个循环跳过了其余的定界符,并在下一代币的开头停下来。
This should work perfectly for string (or single character) delimiters. Don't forget to include
#include <sstream>
.The first while loop extracts a token using the first character of the string delimiter. The second while loop skips the rest of the delimiter and stops at the beginning of the next token.
以防万一将来,某人想要 vincenzo pii 的答案
, bugs,以便该函数在开始或字符串的末尾有分界符,则不会返回空字符串
Just in case in the future, someone wants out of the box function of Vincenzo Pii 's answer
I also fixed some bugs so that the function won't return an empty string if there is a delimiter at the start or the end of the string
这是我对此的看法。它处理边缘情况并采用可选参数以从结果中删除空条目。
例子
Here's my take on this. It handles the edge cases and takes an optional parameter to remove empty entries from the results.
Examples
这类似于其他答案,但使用
String_view
。因此,这些只是原始字符串的视图。类似于C ++ 20示例。尽管这将是C ++ 17示例。 (编辑到跳动空匹配)This is similar to other answers but it's using
string_view
. So these are just views for the original string. Similar to the c++20 example. Though this would be a c++17 example. (edit to skip empty matches)