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. How can we remove space only from the end of string?
Forum Updated to NodeBB v4.3 + New Features

How can we remove space only from the end of string?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 7.5k Views 1 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    ex - @Hello Qt . . .@
    The number of space may vary.

    Pratik Agrawal

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      There is no build-in rtrimmed or ltrimmed function that I am aware of. So, easiest way is to just create a simple function that does that yourself.

      It could be as simple as (brain to terminal, not tested)
      @
      QString rtrimmed(const QString input)
      {
      QString result = input;
      while (result.count() > 0 && result[result.count()-1)].isSpace() ) {
      result = result.left(result.count() -1 );
      }

      return result;
      }
      @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        Since trimmed removes also spaces at the beginning, you have to do a loop over each character in your string and then truncate:

        @
        int truncateAt = myString.size();
        for( ; truncateAt > 0; truncateAt-- )
        if( ! QChar.isSpace( myString.at( truncateAt ) )
        break;

        myString.truncate( truncaetAt );
        @

        (forum written code, not tested)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          I would go with a regex:

          @
          QString ttt = " Qt . . . ab ";
          qDebug() << "ttt" << ttt;
          ttt.replace(QRegExp("\s*$"), "");
          qDebug() << "tt2" << ttt;

          // output:
          // ttt " Qt . . . ab "
          // tt2 " Qt . . . ab"
          @

          http://www.catb.org/~esr/faqs/smart-questions.html

          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