Get the string between two delimiters only.
-
Hi,
I have this sample string
QString str = "$23,10000:SSIBRDBUNO,10001:AUG2101001,40010:374,40030:320,20050:100,20060:100,20070:200,20080:1,:,200A3:234,10002:01082021,10003:01082021,10010:0000001,10011:0000000,10022:0000001,10030:1,10031:01082021,10032:01082021,10060:1,10070:1@3237472528!";I want to get all the strings only between ':' and ',' delimiters. I have used the split function but I am also getting the characters in between ',' and ':'.
Example output should be:
SSIBRDBUNO
AUG2101001
374
320
100
100
etc.Any help would be appreciated.
Thank you.
-
Hi @mbatra,
I want to get all the strings only between ':' and ',' delimiters.
Here's two options.
The first uses
QString::split()
to split on the:
and then trims everything after the,
:const auto parts = str.split(QLatin1Char(':')).mid(1); for (const auto &part: parts) { const QString item = part.left(part.indexOf(QLatin1Char(','))); qDebug() << item; }
Option 2 uses regex to define the actual content you want to capture:
const static QRegularExpression pattern(QStringLiteral(":([^,]*),")); for (auto iter = pattern.globalMatch(str); iter.hasNext(); ) { qDebug() << iter.next().captured(1); }
They are both identical for your test string, both outputting:
"SSIBRDBUNO" "AUG2101001" "374" "320" "100" "100" "200" "1" "" "234" "01082021" "01082021" "0000001" "0000000" "0000001" "1" "01082021" "01082021" "1"
But they will differ on some input corner cases. Which one is "more correct" will depend on your specific use case/s.
Cheers.
-
Hi @mbatra,
I want to get all the strings only between ':' and ',' delimiters.
Here's two options.
The first uses
QString::split()
to split on the:
and then trims everything after the,
:const auto parts = str.split(QLatin1Char(':')).mid(1); for (const auto &part: parts) { const QString item = part.left(part.indexOf(QLatin1Char(','))); qDebug() << item; }
Option 2 uses regex to define the actual content you want to capture:
const static QRegularExpression pattern(QStringLiteral(":([^,]*),")); for (auto iter = pattern.globalMatch(str); iter.hasNext(); ) { qDebug() << iter.next().captured(1); }
They are both identical for your test string, both outputting:
"SSIBRDBUNO" "AUG2101001" "374" "320" "100" "100" "200" "1" "" "234" "01082021" "01082021" "0000001" "0000000" "0000001" "1" "01082021" "01082021" "1"
But they will differ on some input corner cases. Which one is "more correct" will depend on your specific use case/s.
Cheers.
Hi Paul,
Thanks for your response. Its working fine with both the methods. Just one more help, the first method is fine for me:
const auto parts = str.split(QLatin1Char(':')).mid(1);
for (const auto &part: parts) {
const QString item = part.left(part.indexOf(QLatin1Char(',')));
qDebug() << item;
}as I need the last string "1@3237472528!" also. But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.
Thanks.
-
Hi Paul,
Thanks for your response. Its working fine with both the methods. Just one more help, the first method is fine for me:
const auto parts = str.split(QLatin1Char(':')).mid(1);
for (const auto &part: parts) {
const QString item = part.left(part.indexOf(QLatin1Char(',')));
qDebug() << item;
}as I need the last string "1@3237472528!" also. But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.
Thanks.
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.
-
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.
@Paul-Colby
Hi Paul,Thank you very much for your response.
@Paul-Colby said in Get the string between two delimiters only.:
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:
No, '@' will not appear elsewhere in the string. This worked fine.
Thanks.
-
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....
-