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. Replacing words in a string
Forum Update on Monday, May 27th 2025

Replacing words in a string

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 9.9k 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.
  • G Offline
    G Offline
    GrahamL
    wrote on 28 Oct 2013, 16:00 last edited by
    #1

    Hi
    If I have a string -
    @
    QString s = "Some text MyWord some more text MyWordAgain"
    @
    Now I want to replace occurrences of 'MyWord' but ONLY where that is the whole word, so that in the above example 'MyWordAgain' would not be replaced.
    I tried
    @
    s.replace("MyWord","AnotherWord")
    @
    but obviously it replaced both occurrences.
    How can I achieve this?

    Thanks

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NicuPopescu
      wrote on 28 Oct 2013, 17:27 last edited by
      #2

      -use QString::indexOf to find the index of the first occurrence
      -use QString & QString::replace ( int position, int n, const QString & after )

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Oct 2013, 20:30 last edited by
        #3

        Hi,

        You can also use QRegularExpression/QRegExp depending whether you're using Qt 5 or Qt 4

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrahamL
          wrote on 29 Oct 2013, 08:17 last edited by
          #4

          Hi
          I tried with regular expressions but cannot get a match
          @
          QRegualrExpression regExp("\bMyWord");
          str.replace(regExp,"Another");
          @
          Can anyone suggest what is wrong with my regular expression?

          Thanks

          1 Reply Last reply
          0
          • I Offline
            I Offline
            IamSumit
            wrote on 29 Oct 2013, 08:53 last edited by
            #5

            Try it ...
            QString str="Some text MyWord some more text MyWordAgain";
            s.replace(" MyWord "," AnotherWord "); //both strings having one character spaces before and after .

            Be Cute

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrahamL
              wrote on 29 Oct 2013, 08:55 last edited by
              #6

              Thanks all

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NicuPopescu
                wrote on 29 Oct 2013, 10:04 last edited by
                #7

                bq. Try it …
                QString str=“Some text MyWord some more text MyWordAgain”;
                s.replace(” MyWord “,” AnotherWord “); //both strings having one character spaces before and after .

                this is a particular case ... what if you'd want to replace substring within a word or the substring to replace is at the beginning of the string(without leading space character)?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GrahamL
                  wrote on 29 Oct 2013, 10:31 last edited by
                  #8

                  Ok So my problem is not solved
                  I need to replace all occurrences of a word in a string
                  So I created a QRegularExpression -
                  @
                  QRegualrExpression regexp("\bWord");
                  QString s = "this Word is to be replaced and this Word but not this Wordaaaa";
                  s.replace(rexexp,"NewWord");
                  @
                  This has the effect of setting s to
                  @
                  this NewWord is to be replaced and the NewWord but not this NewWordaaaa
                  @
                  How can I stop the last substitution being made?

                  Thanks again

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    NicuPopescu
                    wrote on 29 Oct 2013, 14:03 last edited by
                    #9

                    try this:

                    @QRegualrExpression regexp("\bWord\b");@

                    1 Reply Last reply
                    0

                    1/9

                    28 Oct 2013, 16:00

                    • Login

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