Get the string between two delimiters only.
-
as I need the last string "1@3237472528!"
Oh right, the regex version didn't include that entry. To make them identical, the regex should have been:
const static QRegularExpression pattern(QStringLiteral(":([^,]*)(,|$)"));
But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.
Well, it depends a bit on how definitive your example is... ie can random
@
characters appear elsewhere in the string? It doesn't appear so (in your sample string), in which case either of these will do the trick:Split version:
const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
Regex version:
const static QRegularExpression pattern(QStringLiteral(":([^,]*)[,@]"));
Again, which approach is better depends on the other possible variations of your input.
Cheers.
Hi Paul,
Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:
QFile file("D:\data.csv");
if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
{
QTextStream out(&file);
out << query7; // query7 is a QString.
}But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.
QFile file("D:\\v1.7\\data.csv"); file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ); const auto parts = str.split(QLatin1Char(':')).mid(1); for (const auto &part: parts) { //const QString item = part.left(part.indexOf(QLatin1Char(','))); const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@'))); qDebug() << "item :"<<item; QTextStream out(&file); out << item; }
Can you help me to resolve this issue.
Thanks.
-
Hi Paul,
Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:
QFile file("D:\data.csv");
if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
{
QTextStream out(&file);
out << query7; // query7 is a QString.
}But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.
QFile file("D:\\v1.7\\data.csv"); file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ); const auto parts = str.split(QLatin1Char(':')).mid(1); for (const auto &part: parts) { //const QString item = part.left(part.indexOf(QLatin1Char(','))); const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@'))); qDebug() << "item :"<<item; QTextStream out(&file); out << item; }
Can you help me to resolve this issue.
Thanks.
@mbatra said in Get the string between two delimiters only.:
one column
One column or one line? If one line simply write a new line into the file after each line.
Also, why do you create QTextStream instance inside the loop? This is wasting CPU cycles for nothing. -
@mbatra said in Get the string between two delimiters only.:
one column
One column or one line? If one line simply write a new line into the file after each line.
Also, why do you create QTextStream instance inside the loop? This is wasting CPU cycles for nothing. -
Hi,
One String in each column. I have moved QTextStream outside the loop for now.
But All strings are comin in one column only. -
@mbatra
Look in the.csv
file, not what Excel or whatever spreadsheet you are using shows you. How are the columns separated in the file? Did you output space, comma or tab or whatever you want/need to separate the strings into columns? "CSV" stands for "comma separated values". In English you need to put commas between the columns, but I have seen in other posts that if your language uses,
in floating point numbers for English decimal point (.
) then I believe you are supposed to use some other separator. -
Hi Paul,
Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:
QFile file("D:\data.csv");
if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
{
QTextStream out(&file);
out << query7; // query7 is a QString.
}But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.
QFile file("D:\\v1.7\\data.csv"); file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ); const auto parts = str.split(QLatin1Char(':')).mid(1); for (const auto &part: parts) { //const QString item = part.left(part.indexOf(QLatin1Char(','))); const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@'))); qDebug() << "item :"<<item; QTextStream out(&file); out << item; }
Can you help me to resolve this issue.
Thanks.
@mbatra said in Get the string between two delimiters only.:
part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
I'm not sure this is correct: you're calling left() and then on the result of that call you again call left but use result from indexOf() from the whole string not the part returned from first left() call.
Does qDebug() << "item :"<<item; output what you want? -
@mbatra said in Get the string between two delimiters only.:
part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
I'm not sure this is correct: you're calling left() and then on the result of that call you again call left but use result from indexOf() from the whole string not the part returned from first left() call.
Does qDebug() << "item :"<<item; output what you want? -
Yes, it outputs as expected. All the strings.
But when I write the strings into .csv file, it enters aal the string into one column. -
-
@jsulm said in Get the string between two delimiters only.:
Do you actually write any separator like ',' to separate the columns?
The code indicates that the OP does not....
-