Refer to "Search & Replace function for LoadRunner":
http://ptfrontline.wordpress.com/2009/03/13/search-replace-function-for-lr/
There is no direct function in LoadRunner that supports finding and replacing strings, so you can encapsulate a lr_replace function:
// ----------------------------------------------------------------------------
//
Description:
Search for and replace text within a string.
//
Parameters:
SRC (in)-pointer to source string
From (in)-pointer to search text
to (on)-pointer to replacement text
//
Returns:
Returns A pointer to dynamically-allocated memory containing string
With occurences of the text pointed through ' from ' replaced
The text pointed to by ' to '.
//
Notes:
Do not use the directly in scripts unless
User who knows C and string handling. See below for the function you
Should use!
//
// ----------------------------------------------------------------------------
Char *strreplace (const char *SRC, const char *from, const char *to)
{
Char *value;
Char *dst;
Char *match;
int size;
int Fromlen;
int Tolen;
Find out the lengths of the source string, text to replace, and
The replacement text.
size = strlen (src) + 1;
Fromlen = strlen (from);
Tolen = strlen (To);
Allocate the first chunk with enough for the original string.
Value = (char *) malloc (size);
We need to return ' value ', so let's make a copy to mess around with.
DST = value;
Before we begin, let's see if malloc is successful.
if (value! = NULL)
{
Loop until no matches is found.
for (;;)
{
Try to find the search text.
Match = (char *) strstr (SRC, from);
if (match! = NULL)
{
Found search text at location ' match '.
Find out how many characters to copy the ' match '.
size_t count = match-src;
We is going to realloc, and for that we'll need a
Temporary pointer for safe usage.
Char *temp;
Calculate The total size the string would be is after the
Replacement is performed.
Size + = Tolen-fromlen;
Attempt to realloc memory for the new size.
//
temp = realloc (value, size);
temp = (char *) realloc (value, size);
if (temp = = NULL)
{
//attempt to realloc failed. Free the previously malloc ' d
//memory and return with our Tail between our legs.
Free (value);
return NULL;
}
//ReAlloc was successful. But we'll want to
//Return ' value ' Eventually, so let's point it to the MEM Ory
//That we is now working with. And let's not forget to point
/to the right location in the destination as Well.
DST = temp + (dst-value);
value = temp;
Copy from the source to the point where we matched. Then
Move the source pointer ahead by the amount we copied. and
Move the destination pointer ahead by the same amount.
Memmove (DST, SRC, count);
src + = count;
DST + = count;
Now copy in the replacement text "to" at the position of
The match. Adjust the source pointer by the text we replaced.
Adjust the destination pointer by the amount of replacement
Text.
Memmove (DST, to, Tolen);
src + = Fromlen;
DST + = Tolen;
}
else//No match found.
{
Copy any remaining part of the string. This includes the null
termination character.
strcpy (DST, SRC);
Break
}
}//For loop ()
}
return value;
}
// ----------------------------------------------------------------------------
//
Description:
Find and replace text within a LoadRunner string.
//
Parameters:
Lrparam (in)-pointer to LoadRunner Parameter Name
Findstr (in)-pointer to the text top search for
Replacestr (in)-pointer to text to use as replacement
//
Returns:
Returns an integer. 0=error, 1=success.
//
Example:
Lr_save_string ("This is a small test of the search and replace function", "Lrparam");
Lr_replace ("Lrparam", "a", "-x-");
Lr_output_message ("%s", Lr_eval_string ("{Lrparam}"));
//
// ----------------------------------------------------------------------------
int lr_replace (const char *lrparam, Char *findstr, char *replacestr)
{
int res = 0;
Char *result_str;
Char lrp[1024];
Finalize the LR Param Name
sprintf (LRP, "{%s}", Lrparam);
Do the Search and Replace
Result_str = Strreplace (lr_eval_string (LRP), findstr, REPLACESTR);
Process Results
if (result_str! = NULL)
{
Lr_save_string (Result_str, Lrparam);
Free (RESULT_STR);
res = 1;
}
return res;
}//EOF
Referring to the header file "Lr_replace.h" in the script that includes the above code, the example using the Lr_replace function is as follows:
#include "lr_replace.h"
Action ()
{
Store a string into "Mypar" parameter
Lr_save_string ("This is a string", "Mypar");
For examples sake, convert it to URL encoded format
Web_convert_param ("Mypar",
"Sourceencoding=plain",
"Targetencoding=url", last);
Output the current result
Lr_output_message ("%s", Lr_eval_string ("{mypar}"));
Replace the? Characters with%20
Lr_replace ("Mypar", "+", "%20");
Output New Result
Lr_output_message ("%s", Lr_eval_string ("{mypar}"));
return 0;
}
Find and replace strings in LoadRunner