Splitting QString On Spaces And Keeping The Space In QList - Best Or 'canonical' Way
As in the title, I would like to ask what is the best way to split a QString on spaces and - where relevant - keep the spaces as parts of the resulting QList elements. I'm interested in the most efficient method of doing this, considering modern C++ and Qt >= 6.0 paradigms.
For the purpose of this question I will replace normal spaces with '_' - I hope it makes the problem easier to understand.
Imagine the following code:
QString myString = "This_is_an_exemplary_text";
for (auto word : myString.split('_')) {
qDebug() << word;
}
The output of the code would be:
"This" "is" "an" "exemplary" "text"
Information about the spaces was lost in the splitting process. Is there a smart way to preserve it, and have the following output:
"This_" "is_" "an_" "exemplary_" "text"
Any suggestion would be welcomed.
Answer
Consider myString.split(QRegularExpression("(?<= )")
The regular expression says "an empty substring preceded by a space", using the positive look-behind syntax.
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?