Get last element of QStringList

I have a QString List like this

daily-logs.06-01-2015.01 
daily-logs.06-01-2015.02 
daily-logs.06-01-2015.03

Now I would like to get new QStringList get the number last of the string like this

01 02 03:

QStringList list = folder_file.entryList(nameFilter);

for(int i=0;i<list.count();i++){
    qDebug()<<list.at(i).toLocal8Bit().constData();


    QStringList list2=list.at(i).split(".");
    for(int j=0;j<list2.count();j++){
        //qDebug()<<list2.at(j).toLocal8Bit().constData();
        QString list3=list2.value(list2.length()-1);

        qDebug()<<list3;
        //qDebug()<<list3;
    }

I am able to get only the last value. But I think this is more for loop. So does anyone have a method to get this more performance.

Upvotes: 2

Views: 10353

Answers (3)

Mykhailo Iaremenko
Mykhailo Iaremenko

Reputation: 63

QStringList list = folder_file.entryList(nameFilter);
for (int i = 0; i < list.count(); ++i) {
    QStringList wordList = list[i].split('.');
    qDebug() << wordList.takeLast();
}

Maybe it is not so cute and short as it could be but it is an example without QFileInfo.

Upvotes: 2

user2672165
user2672165

Reputation: 3049

Use special file QFileInfo::suffix function:

const auto& list = folder_file.entryList(nameFilter);
QStringList output;   
for(const auto& f : list)   
    output << QFileInfo(f).suffix();

Upvotes: 6

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

Your code is too dirty...

QStringList DoSomeDirtyWork( const QStringList& input )
{
  QStringList output;

  for ( auto it = input.constBegin(); it != input.constEnd(); ++it )
  {
    const QString& src = *it;
    const int pos = src.lastIndexOf('.');
    output << src.mid( pos );
  }

  return output;
}

Upvotes: 3

Related Questions