Search part of a QString in QStringList in Qt - c++

In QString, contains() method works like this:
QString s = "long word";
s.contains("long"); // -> True
I would assume that QStringList works similarly, but it does not:
QStringList s;
s << "long word";
s << "longer word";
s.contains("long"); // -> False
QStringList contains searches for the exact match, which does not work like I want it to. Is there an easy way to find a part of a string in a QStringList? I could of course loop through the QStringList and use contains() there, but is there a better way?

You can use the function QStringList::filter() :
QStringList QStringList::filter(const QString &str,
Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns a list of all the strings containing the substring str.
and check that the list returned is not empty.
In your case:
QStringList s;
s << "long word";
s << "longer word";
s.filter("long"); // returns "long word", and also "longer word" since "longer" contains "long"

Related

How to add items from multiple QStringLists to one?

If I have several QStringLists, for example:
QStringList str1 = {"name1", "name2", "name3"};
QStringList str2 = {"value1", "value2", "value3"};
QStringList str3 = {"add1", "add2", "add3"};
Is there any way to get list of lists (nested list) like QStringList listAll; that will look like this:
(("name1","value1","add1"),("name2","value2","add2"),("name3","value3","add3"))
From the comment, it looks like you are trying pack the list of strings to a QVector instead of QList. In that case, do simply iterate through the QStringList s and append the list of strings created from the equal indexes to the vector.
#include <QVector>
QVector<QStringList> allList;
allList.reserve(str1.size()); // reserve the memory
for(int idx{0}; idx < str1.size(); ++idx)
{
allList.append({str1[idx], str2[idx], str3[idx]});
}
you dont need a vector for that, keep using the StringList
QStringList str1={"name1", "name2", "name3"};
QStringList str2={"value1", "value2", "value3"};
QStringList str3={"add1", "add2", "add3"};
QStringList r{};
// use elegantly the << operator and the join method instead... 😃
r << str1.join(",") << str2.join(",") << str3.join(",");
qDebug() << "result : " << r.join(";");
//result:
//"name1,name2,name3;value1,value2,value3;add1,add2,add3"

How to parse QString?

I am trying to parse QString with following code:
QString name = "name.bin";
QStringList imgName = name.split(".");
qDebug() << imgName.at(0); // result is "name"
However I need just name without any ("). Then I write another code to manually delete quotes("").
Is there any easy way to split QString?
There are no " " in the string. It just contains Name
qDebug() << (imgName.at(0))[0]; // result is n

How to print string literal and QString with qDebug?

Is there any easy way to get the following work? I mean is there any helper class in Qt which prepares the string for qDebug?
QString s = "value";
qDebug("abc" + s + "def");
You can use the following:
qDebug().nospace() << "abc" << qPrintable(s) << "def";
The nospace() is to avoid printing out spaces after every argument (which is default for qDebug()).
No really easy way I am aware of. You can do:
QByteArray s = "value";
qDebug("abc" + s + "def");
or
QString s = "value";
qDebug("abc" + s.toLatin1() + "def");
According to Qt Core 5.6 documentation you should use qUtf8Printable() from <QtGlobal> header to print QString with qDebug.
You should do as follows:
QString s = "some text";
qDebug("%s", qUtf8Printable(s));
or shorter:
QString s = "some text";
qDebug(qUtf8Printable(s));
See:
http://doc.qt.io/qt-5/qtglobal.html#qPrintable
http://doc.qt.io/qt-5/qtglobal.html#qUtf8Printable
Option 1: Use qDebug's default mode of a C-string format and variable argument list (like printf):
qDebug("abc%sdef", s.toLatin1().constData());
Option 2: Use the C++ version with overloaded << operator:
#include <QtDebug>
qDebug().nospace() << "abc" << qPrintable(s) << "def";
Reference: https://qt-project.org/doc/qt-5-snapshot/qtglobal.html#qDebug
Just rewrite your code like this:
QString s = "value";
qDebug() << "abc" << s << "def";
I know this question is a bit old, but it appears nearly on top when searching for it in the web. One can overload the operator for qDebug (more specific for QDebug) to make it accept std::strings like this:
inline QDebug operator<<(QDebug dbg, const std::string& str)
{
dbg.nospace() << QString::fromStdString(str);
return dbg.space();
}
This thing is for years in all of my projects, I nearly forget it is still not there by default.
After that, usage of << for qDebug() is a lot more usable imho. You can even mix QString and std::string. Some additional(but not really intended) feature is, that you sometimes can throw in integers or other types that allow implicit conversion to std::string .

how to check for a particular string in another source string

Can anybody please let me know how to check for a particular string in antoher string using VC++ 2003?
For Eg:
Soruce string - "I say that the site referenced here is not in configuration database. need to check";
String to find - "the site referenced here is not in configuration database"
can anybody please help me with this? Let me know if anymore clarity required.
std::string::find might meet what you want, see this
string s1 = "I say that the site referenced here is not in configuration database. need to check";
string s2 = "the site referenced here is not in configuration database";
if (s1.find(s2) != std::string::npos){
std::cout << " found " << std::endl;
}
string sourceString = "I say that the site referenced here is not in configuration database. need to check";
string stringToFind = "the site referenced here is not in configuration database";
sourceString.find(stringToFind);
This method call will return you the postion of type size_t
Hope this helps you
For C/C++, you can use strstr():
const char * strstr ( const char * str1, const char * str2 );
Locate substring
Returns a pointer to the first occurrence of str2 in
str1, or a null pointer if str2 is not part of str1.
If you insist on pure C++, use std::string::find:
size_t find (const std::string& str, size_t pos);
Find content in string
Searches the string for the content specified
in either str, s or c, and returns the position of the first
occurrence in the string.

QString splitting multiple delimiters

I'm having trouble splitting a QString properly. Unless I'm mistaken, for multiple delimiters I need a regex, and I can't seem to figure out an expression as I'm quite new to them.
the string is text input from a file:
f 523/845/1 524/846/2 562/847/3 564/848/4
I need each number seperately to put into an array.
Some codes....
QStringList x;
QString line = in.readLine();
while (!line.isNull()) {
QRegExp sep("\\s*/*");
x = line.split(sep);
Any pointers?
Cheers
Change your regular expression like this:
QRegExp sep("(\\s+|/)");
then x will have every number.
I found it quite useful to try out RegEx's interactively. Nowadays there are a lot of online tools even, for example: http://gskinner.com/RegExr/
You can put your search text there and play with the RegEx to see what is matched when.
You could use the strtok function, which split a QString with one or more different tokens.
It would be like this:
QString a = "f 523/845/1 524/846/2 562/847/3 564/848/4";
QByteArray ba = a.toLocal8Bit();
char *myString = ba.data();
char *p = strtok(myString, " /");
while (p) {
qDebug() << "p : " << p;
p = strtok(NULL, " /");
}
You can set as many tokens as you need. For further info visit the cplusplus page of this particular function. http://www.cplusplus.com/reference/cstring/strtok/
Regards!.