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::right does not work
Forum Updated to NodeBB v4.3 + New Features

QString::right does not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 2.5k Views 5 Watching
  • 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by
    #1

    This code:

    int split = filePath.lastIndexOf(QRegExp("/|\\\\"));
    QString test1 = filePath.left(split);
    QString test2 = filePath.right(split);
    std::cout << filePath.toStdString() << std::endl
    	<< test1.toStdString() << std::endl
    	<< test2.toStdString() << std::endl;
    

    has this output:

    D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
    D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh
    Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
    

    Why is it not??

    D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
    D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh
    /ic_helmet.msh
    
    kshegunovK 1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      left() and right() take number of characters, not offset of the divide. So left(n) returns n leftmost characters and right(n) returns n rightmost characters, not characters after n.
      What you want here is right(filePath.size() - split).

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Well it does work
        so most likely something with the input.

        QString x = "Pineapple";
        QString y = x.right(5); // y == "apple"

        Right() will
        "Returns a substring that contains the n rightmost characters of the string."

        But you give it the INDEX of the last seen. Not the count of chars you want from the right.

        update: Chris beat me to it :)

        1 Reply Last reply
        1
        • QT-static-prgmQ Offline
          QT-static-prgmQ Offline
          QT-static-prgm
          wrote on last edited by
          #4

          i feel stupid

          raven-worxR 1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Well any productive programmer
            will make such kind of errors from time to time.

            My all time DOH is the classic

            class X {
            MyClass *mine;
            }

            and then in constructor
            MyClass *mine = new MyClass();

            and then some other place crash on
            mine->func();

            I still do that sometimes.. even after like 25 years..

            1 Reply Last reply
            0
            • QT-static-prgmQ QT-static-prgm

              i feel stupid

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @QT-static-prgm said in QString::right does not work:

              i feel stupid

              the Qt docs are written pretty good, and can save alot of time in such cases ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • QT-static-prgmQ QT-static-prgm

                This code:

                int split = filePath.lastIndexOf(QRegExp("/|\\\\"));
                QString test1 = filePath.left(split);
                QString test2 = filePath.right(split);
                std::cout << filePath.toStdString() << std::endl
                	<< test1.toStdString() << std::endl
                	<< test2.toStdString() << std::endl;
                

                has this output:

                D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
                D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh
                Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
                

                Why is it not??

                D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh/ic_helmet.msh
                D:/workspaces/Visual Studio 2015/Projects/OpenGL/Release/Msh
                /ic_helmet.msh
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                If I may throw my 2 cents:
                Use the filesystem utilities for what you're doing and not regular expressions. Firstly, they're already tested, and secondly they're probably better as they're specialized for such tasks. For example QFileInfo is a good start.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3

                • Login

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