Replace function is used to replace the part of the given string with other sub string in QTP.
Example -
myString = "Sachin plays cricket"
print replace(myString,"Sachin","Dhoni")
'Will print Dhoni plays cricket
print replace(myString,"sachin","Dhoni")
'Will print Sachin plays cricket. Because we have used sachin in small case letter
print replace(myString,"sachin","Dhoni",1,-1,0)
'Will print Sachin plays cricket .....Same as above
print replace(myString,"sachin","Dhoni",1,-1,1)
Syntax -
replace(myString,"replacethis","replacewith",startat,count,comparisonmehtod)
Parameter1 - myString - > String to be scanned and modified
Parameter2 - replacethis- > String to be searched and replaced for
Parameter3 - replacewith- > String to be replaced with
Parameter4 - startat- > from what position the myString should be searched...Default is 1.
Parameter5 - count- > How many replacements should be done. Default is -1 - All occurrences
Parameter6 - comparisonmehtod- > Is comparison case-sensitive?..Default is 0 - binary comparison.
Please give your inputs, suggestions, feedback to Us about above QTP article. We value your thoughts.
Example -
myString = "Sachin plays cricket"
print replace(myString,"Sachin","Dhoni")
'Will print Dhoni plays cricket
print replace(myString,"sachin","Dhoni")
'Will print Sachin plays cricket. Because we have used sachin in small case letter
print replace(myString,"sachin","Dhoni",1,-1,0)
'Will print Sachin plays cricket .....Same as above
print replace(myString,"sachin","Dhoni",1,-1,1)
'Will print Dhoni plays cricket....
'The last parameter of replace determines whether the replacement is case-sensitive or case-insensitive.
'If last parameter is 0, replacement is case-sensitive
'If last parameter is 1, replacement is case-insensitive
replace(myString,"replacethis","replacewith",startat,count,comparisonmehtod)
Parameter1 - myString - > String to be scanned and modified
Parameter2 - replacethis- > String to be searched and replaced for
Parameter3 - replacewith- > String to be replaced with
Parameter4 - startat- > from what position the myString should be searched...Default is 1.
Parameter5 - count- > How many replacements should be done. Default is -1 - All occurrences
Parameter6 - comparisonmehtod- > Is comparison case-sensitive?..Default is 0 - binary comparison.
More interview questions and answers on strings in QTP
- What are the different string functions in QTP?
- How to extract the digits from given string in QTP?
- How to sort the array of strings in qtp
- How to get the first character in string in QTP?
- How to convert the string into upper case in QTP?
- How to get the last character from the string in QTP?
- How to replace the string in qtp?
- How to check if substring exists in QTP?
- Explain right, mid and left functions in qtp?
- How to remove the spaces from string in qtp?
- What are the trim functions in qtp?
- Explain replace function in qtp with example.
- Explain the split function in qtp with example.
- Explain lcase and ucase functions in qtp.
- What is the len function in qtp?
- Give the example of instr function in qtp
No comments:
Post a Comment
Please Leave your reply. We value your feedback and inputs