[Solved]How to use multiple expressions to find matches in QString::endsWith( const QString &, Qt::CaseSensitivity) function?



  • I have list of urls I need find out urls pointing at pictures, I have this code but my problem is it always returns false so no match in list is found (there are 5 matches in my actual list), I tryed it with QRegExp but there is no function QString::endsWith(QRegExp) or something. I see the problem is with multiple expressions because it works ok if fileType=".png". I go afk for longer time now so if you can help it could be nice so I can continue with work when i come back. Thanks :)
    @
    int a,b;
    QString string,fileType="(.jpg|.gif|.png|.jpeg|.bmp|.tif|.tiff|.swg|.ico)";
    while(a<UrlList.count()){
    string=UrlList[a];
    if (string.endsWith(fileType, Qt::CaseInsensitive) == 1){ //there is my problem
    FileList.append(string); //this list I'll need later
    UrlList[b]=UrlList[a]; //pushing urls ill need to 1st places in list
    b++;
    }
    a++;
    if(a>=UrlList.count()){while(b<UrlList.count()){UrlList.removeAt(b);}break;} //to remove unneeded urls
    }@



  • Your program thinks, your type ends with the long string.
    I would do it that way:
    [code]
    for(int i=0;i<fileType.split("|").count();i++) {
    if (string.endsWith(fileType.split("|").at(i), Qt::CaseInsensitive) == 1){
    FileList.append(string); //this list I'll need later
    UrlList[b]=UrlList[a]; //pushing urls ill need to 1st places in list
    b++;
    }
    }[/code]
    or something like that.
    split create a QStringList and splits your string. Go through the list and check every file seperately with every fileend.



  • Thanks it worked :)



  • That will fail for ".jpg" and ".ico" files. Further, you're not initializing all your local variables a and b, I'm wondering how it could've ever worked consistently.



  • Yes, that's right, thank you DerManu :)

    Please change
    [code]QString string,fileType="(.jpg|.gif|.png|.jpeg|.bmp|.tif|.tiff|.swg|.ico)";[/code]
    to
    [code]QString string,fileType=".jpg|.gif|.png|.jpeg|.bmp|.tif|.tiff|.swg|.ico";[/code]



  • yes I noticed it right after I read "Your program thinks, your type ends with the long string." so I removed it. And a and b were initialized somewhere else in program so I just typed it there forgot "=0" but in program its @int a=0,b=0;@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.