Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString remove from the end untill a specific character
QtWS25 Last Chance

QString remove from the end untill a specific character

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm trying to find a simple way to do this. I already made something that works, but it's a mess and I think there's a simpler way to do this.

    Here's what I did:

    QString test = "Android/media/com.whatsapp";
    std::reverse(test.begin(), test.end());
    QString ola = test.mid(0, test.indexOf("/"));
    std::reverse(ola.begin(), ola.end());
    int ren = ola.count();
    test.remove(0, ren);
    std::reverse(test.begin(), test.end());
    qDebug() << test; //prints "Android/media/"
    

    Is there an easier way to do this?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #4
      test = test.left(test.lastIndexOf('/') + 1);
      

      or

      test.truncate(test.lastIndexOf('/') + 1);
      

      or

      test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
      
      S 1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        see QString::lastIndexOf()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #3
          QString test = "Android/media/com.whatsapp";
          int position = test.lastIndexOf( QDir::separator() );
          if ( -1 != position ) {
              test.remove( 0, position + 1 );
          }
          
          1 Reply Last reply
          1
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #4
            test = test.left(test.lastIndexOf('/') + 1);
            

            or

            test.truncate(test.lastIndexOf('/') + 1);
            

            or

            test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
            
            S 1 Reply Last reply
            2
            • Chris KawaC Chris Kawa
              test = test.left(test.lastIndexOf('/') + 1);
              

              or

              test.truncate(test.lastIndexOf('/') + 1);
              

              or

              test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
              
              S Offline
              S Offline
              Sucharek
              wrote on last edited by
              #5

              Hi @Chris-Kawa, your solution is probably the shortest and simplest.
              I'll use one of these 3 commands. Thanks

              Thank you for replies from others.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved