Automation Testing

QTP Using the Replace Function to remove Line Feed and Carriage Return characters

By Test Guild
  • Share:
Join the Guild for FREE

How many times have you found yourself debugging buggy QTP code only to find out that there is an extra character, like a space or carriage return, that is causing a string compare not to work?

What is the QTP UFT Replace function?

The replace function is used to return a string in which a specified substring has been replaced with another substring.

The UFT Replace function has the following parameters:

Replace(yourStringThatContainsCharacterToReplace,theCharacterToFind,replaceWithThis)

Some Common Uses of the Replace Function:

  •  How do I replace a space with a non space?:
    • myStringWithSpaces = ‘This Is String With Spaces”
    • myStringNoSpaces = Replace(myStringWithSpaces,” “,””)
    • msgbox myStringNoSpaces

The above string should now equal = “ThisIsStringWithSpaces”

  •    How do I remove double quotes from a string (chr(34) is the ASCII code for dbl quotes)?:
    • myQuote = “Einstein once said “”Imagination is more important than knowledge”””
    • myQuotes = Replace(myQuotes,chr(34),””)
    • msgbox myQuotes
  • How do  I remove a space and a carriage return?
    • The ASCII codes for a space with a carriage return is Chr(10) & Chr(13).
    • yourString = Replace(yourString,Chr(10) & Chr(13),””)

To see a chart of all ASCII QTP CHR() check out: QTP ASCII CHR() CODE CHART

Create a Replace function:

If you need to use the functionality ofter i might help if you created a simple function like the following:

Function myReplace(stringToClean, replaceThis, replaceWithThis)
myReplace = Replace(tringToClean, replaceThis, replaceWithThis, 1, -1, 1)
End Function

  1. Hi,

    I have tried this “myQuotes = Replace(myQuotes,chr(34),””)
    msgbox myQuotes”. But it gives a type mismatch error.

    I have also tried myQuotes = Replace(myQuotes,chr(34),char(0))
    msgbox myQuotes
    But of no use. Can you please tell me the solution?

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Leveraging AI and Playwright for Test Case Generation

Posted on 11/22/2024

Two BIG trends the past few years in the software testing space been ...

Symbolic AI vs. Gen AI: The Dynamic Duo in Test Automation

Posted on 09/23/2024

You've probably been having conversations lately about whether to use AI for testing. ...

8 Special Ops Principles for Automation Testing

Posted on 08/01/2024

I recently had a conversation, with Alex “ZAP” Chernyak about his journey to ...

Sponsor The Industry-Standard E2E Automation Testing Annual Online Event (Limited Spots Left) - Reach Out Now >>