Results 1 to 9 of 9

Thread: StringBuilder

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default StringBuilder

    Hello, I must append/insert few stuff in a StringB and then print it out; this my code:
    Qt Code:
    1. StringBuilder text = new StringBuilder();
    2. StringBuilder textBefore = new StringBuilder();
    3. while (something) { //do it many times
    4. text.append("something");
    5. textBefore.append("Other");
    6. }
    7.  
    8. String result = text.insert(1, textBefore.toString()).toString();
    To copy to clipboard, switch view to plain text mode 
    Now, I'm looking for a better solution, ie. using just one StringB is it possible for you?
    thanks.
    Regards

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: StirngBuilder

    Well, you could use std::string.insert(), which is a single call. Internally, however, you have to move the portion of the orignal string following the insertion point to a new location; there is no way to accomplish an insertion without breaking the string into separate pieces. All you can do is hide the internals from public view. Even appending onto the end will often involve a copy, unless the string class in question provides additional buffering - wasteful, and ultimately futile, since the buffer size can always be exceeded, requiring a copy again.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: StirngBuilder

    Did you try using std::rope?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [java] Re: StirngBuilder

    sorry, I'm using Java.
    Regards

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [java] Re: StirngBuilder

    Use rope for java then
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: StringBuilder

    WHy; better? Can't I simplify that using StringBuilder?
    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: StringBuilder

    Quote Originally Posted by mickey View Post
    WHy; better?
    Faster.

    Can't I simplify that using StringBuilder?
    You can do whatever you want, I don't care I thought you had a problem with using StringBuilder.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: StringBuilder

    I was only looking for a way to do that using a stringBuilder instead two.
    Regards

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: StringBuilder

    I'm not sure what you are doing but you can have an array of strings that you fill and then assemble the whole string once you have all the elements in the array. If you want to concatenate values of two string builders then obviously you need two string builders
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.