What is the reason that QLatin1String class has not any operations for searching substring in it, such as indexOf
, find
or contains
? Only startsWith
and endsWith
I was found, but they can't search in the middle of the string
Asked
Active
Viewed 55 times
1 Answers
1
As to why, you'd have to take that up with Qt.
Generally speaking QLatin1String
is used to indicate what we used to call ASCII text (no Unicode). QString
is the default, all-powerful string in Qt, and you should generally use that, or you could convert it to a std::string
and use that. Either of these may come at the cost of a copy.
If you want to avoid the copy, QLatin1String
does have begin()
and end()
functions, so you can use standard algorithms on it too, like C++17's new std::search()
:
auto s = QLatin1String("The lazy dog");
auto needle = QLatin1String("azy");
auto it = std::search( std::begin(s), std::end(s),
std::boyer_moore_horspool_searcher( std::begin(needle), std::end(needle) ) );
Here's a working sample using std::string
s that will also work for QLatin1String
s on Coliru.
See more on Bartek's coding blog.

metal
- 6,202
- 1
- 34
- 49