Slice Qstring from right hand side - c++

I have a qstring of directory structure and need to take the second last element after "/" into a new qstring
"C:/Users/emb/Documents/AutoConnectTest/02/Job_0"
How to save 02 into a new QString

You can use QString split function.
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
this code split all section by "/" and merge two second from last.
QString string = "C:/Users/emb/Documents/AutoConnectTest/02/Job_0";
QStringList lst = string.split('/');
qDebug() << lst[lst.count()-2] + "/" + lst[lst.count()-1];
Output:
"02/Job_0"

Related

Pack QStringList to QString and unpack it back

I'm in search for an easy and foolproof way to convert an arbitrary QStringList to a single QString and back.
QStringList fruits;
fruits << "Banana", "Apple", "Orange";
QString packedFruits = pack(fruits);
QStringList unpackFruits = unpack(packedFruits);
// Should be true
// fruits == unpackFruits;
What might be the easiest solution for this kind of problem?
From QStringList to QString - QStringList::join:
Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).
QString pack(QStringList const& list)
{
return list.join(reserved_separator);
}
From QString to QStringList - QString::split:
Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.
QStringList unpack(QString const& string)
{
return string.split(reserved_separator);
}
Previous answers mentioned QString::split and QStringList::join which is the correct way, but if the separator you choose is included in any of the strings it will break your conversion.
You must prevent strings in the list to contain your separator with one of the following techniques:
Throw an error before QStringList::join if any string includes the separator
Ensure they can not contain the separator (for example storing the string with its QByteArray::toHex(myString.toLatin1()) representation, then using a separator that has character(s) outside of the range 0..9 and a..f. Then convert back with QString::fromLatin1(QByteArray::fromHex(myHexString)) afterward
Use any separator regardless if the strings contain it, but implement an escape logic for it before the join(), and an un-escape logic after the split(), so that the separator is never present in any of the strings at the time of join, but all instances of it will be restored.
Use QStringList::join() :
QStringList strList;
strList << "Banana" << "Apple" << "Orange" ;
QString str = strList.join(""); // str = "BananaAppleOrange";
str = strList.join(","); // str = "Banana,Apple,Orange";

Regex - Private subtags RFC5646

Can someone please help me with a regex to pull out subtags from a RFC5646?
Example strings
en-us-x-test-test1 = test,test1
en-gb-x-test-test2 = test,test2
fr-x-test-test3 = test,test3
I'm using a QRegExp
Thanks for any assistance
You don't need a regex here. Split your input by - then take the last two string and add a coma in between:
QString str = "en-us-x-test-test1";
QStringList list = str.split('-');
QString output = list.at(list.count()-2) + "," + list.at(list.count()-1);
Of course, you have to check for list length to avoid index error.

QString split() function with QRegExp strange behaviour

I have some text like this:
"1.801908\t20.439980\t\r\n25.822865\t20.439305\t\r\n26.113739\t4.069647\t\r\n1.800252\t4.301690\t\r\n"
I want to split this text by lines and then by tabs. I am using QString split() function and QRegExp to do that in this way:
QStringList rows = text.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
QStringList cols = rows.at(0).split(QRegExp("[ \t]"), QString::SkipEmptyParts);
But what I got in cols is just one item:
"1.801908\920.439980\9"
As I understand, the first split replaced all the \t characters with \9. But I don't understand why and how to fix that. Any explanation?

c++ find text in QStringList that starts with "..." using .indexOf

I have a question concerning QStringList:
I have a .txt-File containing several 1000 lines of Data followed by this:
+-------------------------+-------------------+-----------------------|
| Conditions at | X1 | X2 |
+-------------------------+-------------------+-----------------------|
| Time [ms] | 0.10780E-02 | 0.27636E-02 |
| Travel [m] | 0.11366E+00 | 0.18796E+01 |
| Velocity [m/s] | 0.43980E+03 | 0.13920E+04 |
| Acceleration [g] | 0.11543E+06 | 0.20936E+05 |
…
Where the Header (Conditions at…) and the first column (Travel, Time,…) always stay the same but the values vary for each run. From this File I want to read the values (only!) into fields of a GUI.
First I write all data into a QStringList. (Each line of .txt copied to one Element of QStringList)
To get the values, from the QStringList I tried to find the corresponding lines with “.indexOf()" which didn´t work because I have to ask for the exact text of the whole line. Since the values vary, the lines are different for each run and my program is not able to find corresponding lines.
Is there a command like “.indexOf-Starting with certain text” which would find me the lines starting with a certain text for example “| Time [ms]”
Thank you very much
itelly
Yes there is method “.indexOf-Starting with certain text”. You can use regular expressions to match the beggining of a string:
int QStringList::indexOf (const QRegExp& rx, int from = 0) const
Use it in this way:
int timeLineIndex = stringList.indexOf(QRegExp("^\| Time \[ms\].+"));
^ means that this text should be at the beggining of a string
\ escapes special characters
.+ means that any text can follow this
EDIT:
Here is a working example that show how it works:
QStringList stringList;
stringList << "abc 5234 hjd";
stringList << "bnd|gf dfs aaa";
stringList << "das gf dfs aaa";
int index = stringList.indexOf(QRegExp("^bnd\|gf.+"));
qDebug() << index;
Output: 1
EDIT:
Here is a function for ezee usage of this:
int indexOfLineStartingWith(const QStringList& list, const QString& textToFind)
{
return list.indexOf(QRegExp("^" + QRegExp::escape(textToFind) + ".+"));
}
int index = indexOfLineStartingWith(stringList, "bnd|gf"); //it's not needed to escape characters here
First of all your actual data starts from the line 4 (excluding the header). Second - each data string has specific layout, that you can parse. Assuming that you read the whole file into the QStringList, where each item in the list represents each line, you can do the following:
QStringList data;
[..]
for (int i = 3; i < data.size(); i++) {
const QString &line = data.at(i);
// Parse the X1 and X2 columns' values
QString strX1 = line.section('|', 1, 1, QString::SectionSkipEmpty).trimmed();
QString strX2 = line.section('|', 2, 2, QString::SectionSkipEmpty).trimmed();
}

How to concatenate QString and int

I am trying to set the text that appears on a tab to something like this
~Untitled(n)
Where "n" is the index of the tab. I am having trouble concatenating the string and integer. This is what I have tried.
armaTab->addTab(new QWidget, "~Untitled (" + QString(armaTab->currentIndex() + 1) + ")");
With that, i end up getting something that looks like this:
~Untitled([])
What is the proper way to concatenate the string and integer to produce the desired result?
"~Untitled (" + QString::number(armaTab->currentIndex() + 1) + ")"
= OR =
QString("~Untitled(%1)").arg(armaTab->currentIndex() + 1)
Try using QString::number(n). This will convert the integer to a QString which you can concatenate to your original string.
QString offers the arg function:
QString("~Untitled %1").arg(armaTab->currentIndex() + 1)
This question already have few good answers. I'm adding another alternate way to concatenate string and integer using stringstream
stringstream ss(str);
ss<<n;
string temp = ss.str();
or we can get the concatenated string by following logic
string temp = str + to_string(n);
Now we can get the QString from string:
QString str = QString::fromUtf8(temp.c_str());