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

A bearded man with blue glasses and a black-and-white jacket smiles at a microphone in a studio setting.

About Joe Colantonio

Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.

Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.

With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.

He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.

Follow him on LinkedIn or check out more at TestGuild.com.

  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"}

What is Regression Testing? (2025 Regression Test Guide)

Posted on 03/30/2025

What is Regression Testing ? Regression testing ensures that new changes—whether they’re bug ...

Why Enterprise Test Automation is Broken—And How to Fix It

Posted on 03/21/2025

Why is Enterprise Test Automation So Hard? Enterprise software testing should accelerate releases, ...

Exploring the Value of AI in Test Case Creation (Pros and Cons)

Posted on 03/10/2025

As testers, we're used to balancing test coverage with tight project deadlines. Right? ...