There are times when you need to use 0 in front of a string in order to meet the formatting requirements of the length.
A function can be encapsulated in LoadRunner to handle this problem:
/*
Function to pad a string to x characters adding the required character at the start of the string
(Note string length Limited by length of the array)
Author:steven Woodcock, Sopra Group
Inputs:
Strchartopad-the String, requires to is padded out i.e. "123456"
Cparamname-the name of the parameter you want the end value to being saved to
Ivallength-the length you want the string to is padded out to i.e. 9
Cvaluetoadd-the character you want to pad the string out with, must is a single character i.e. "0"
Outputs:
Creates a LoadRunner parameter of a name specified in the input ' cParamName '
*/
int Lr_padstr (char* cchartopad, char* cparamname, int ivallength, char* cvaluetoadd) {
Char ctemp[1024] = "";
int iLoop = 0;
int imainloop = 0;
int isubloop = 0;
for (imainloop = 0; imainloop < ivallength; imainloop++) {
if (Imainloop = = strlen (Cchartopad)) {
ILoop = Ivallength-imainloop;
for (isubloop = 0; isubloop < ILoop; isubloop++) {
strcat (Ctemp, Cvaluetoadd);
}
}
}
strcat (Ctemp, Cchartopad);
Lr_save_string (Ctemp, cParamName);
}
LR_PADSTR can make a specified length of a character before the specified string, and then save the modified string in the LoadRunner parameter. Put the above code in the Lr_padstr.h header file and reference it in the LoadRunner script:
#include "Lr_padstr.h"
Action ()
{
For the LR_PADSTR function
char * cshortvalue = "123";
For the sprintf function
Char cpaddedvalue[8] = "";
int ishortvalue = 123;
int i;
The Lr_padstr function, written by Steven Woodcock can is used to pad with zeroes
Lr_padstr (Cshortvalue, "Ppaddedparameter", 8, "0");
Lr_output_message ("Padded value is%s", lr_eval_string ("{ppaddedparameter}"));
The Padtox function can also is used to pad with other characters (e.g. x)
Lr_padstr (Cshortvalue, "Ppaddedparameter", 8, "X");
Lr_output_message ("Padded value is%s", lr_eval_string ("{ppaddedparameter}"));
return 0;
}
Reference:
http://www.bish.co.uk/~richardmjbishop/index.php?option=com_content&view=article&id=85% 3asample-loadrunner-script-pad-a-string-with-leading-zeroes&catid=34%3arecent&itemid=1
LoadRunner string processing-padded string