QLatin1String类没有任何搜索子字符串的操作,例如indexOf
,find
或contains
,这是什么原因?我发现只有startsWith
和endsWith
,但他们不能在字符串的中间搜索
至于为什么,你必须用Qt来解决这个问题。
一般来说,QLatin1String
用于表示我们曾经称之为ASCII文本(没有Unicode)。 QString
是Qt中默认的,全能的字符串,你通常应该使用它,或者你可以将它转换为std::string
并使用它。这些中的任何一个都可能以副本为代价。
如果你想避免副本,QLatin1String
确实有begin()
和end()
函数,所以你也可以在它上面使用标准算法,比如C ++ 17的新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) ) );
这是一个使用std::string
s的工作样本,也适用于QLatin1String
上的Colirus。
在Bartek's coding blog上查看更多信息。