Discussion Board

Results 1 to 5 of 5

Threaded View

  1. #1
    Registered User
    Join Date
    Apr 2008

    Question String search and replace

    I have been making a method for finding a string in HBufC8 and replacing it with a new one.

    void StringReplaceL(HBufC8& aText, 
    const TDesC8& aSearchString, 
    const TDesC8& aReplaceWith)
        TInt searchLength = aSearchString.Length();
        TInt replaceLength = aReplaceWith.Length();
        TPtr8 textPtr = aText.Des();
        TInt pos = 0;
        pos = 0;
        while(pos != KErrNotFound)
            pos = textPtr.Find( aSearchString );
            if(pos != KErrNotFound)
                //Expand the buffer to hold the new data    
                aText.ReAlloc(aText.Length() - searchLength  +  replaceLength);
                textPtr = aText.Des();
                textPtr.Replace(pos, searchLength, aReplaceWith);
    I'd like to get some feedback on possible problems with this code.

    a) It seems that the textPtr.Replace() sometimes causes buffer overflow.

    b) Calling ReAlloc() on all search results doesn't seem like a proper way to do it. I wasn't able to make a loop that would just count the results, as calling
    aText.Mid(pos).Find(aSearchString )
    Always returned me KErrNotFound, even with pos = 0;

    c) Is
    textPtr = aText.Des();
    Necessary after ReAlloc()?

    d) Is there ready example on how to do this efficiently?
    Last edited by Zilppuri; 2009-05-08 at 17:15.

Similar Threads

  1. Find and replace in descriptor.
    By giedi1 in forum Symbian C++
    Replies: 2
    Last Post: 2009-02-20, 09:50
  2. REPLACE on Carbide
    By PACALA_BA in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 5
    Last Post: 2008-07-19, 10:40
  3. Calling cards: Replace "+" with "00" in case of dialling contacts via DTMF
    By cgalli in forum General Development Questions
    Replies: 1
    Last Post: 2007-08-30, 22:01
  4. Personal
    By JSmith77 in forum PersonalJava
    Replies: 4
    Last Post: 2003-07-30, 12:52

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts