how can i put a variable in the middle of the string in loadrunner?
how can i put a variable in the middle of the string in loadrunner?
I have a small difficulty with a script that I am setting in loadrunner in c, I need to put a variable in the middle of a text, how can I do?
Thank you.
3 Answers
3
LoadRunner virtual users are standard C language items for most virtual users. This is where you leverage your foundation skills in the language of your tool to construct the proper string that you need to use. There are many paths to your solution here.
abce{Variable
AsAParameter}efgh and may be use lr_eval_string() for conversion
You can use the standard C function sprintf()
.
sprintf()
You first need to declare a variable with sufficient space, e.g.:
char str[255+1];
The reason I deliberately specify the +1
is to remind others (or even yourself) that C strings terminate with a null character (). So if your string is 3 characters long – e.g.
"abc"
– the string buffer must be at least 4 characters in size.
+1
"abc"
Next, use the sprintf()
function to write to the string buffer. E.g.:
sprintf()
sprintf(str, "Prefix_%s_Suffix", lr_eval_string("{Variable}"));
If your variable happens to be an integer value, then you can replace the %s
with a %d
.
%s
%d
Of course, you can have multiple format specifiers if needed. E.g.:
sprintf(str, "%dt%st%st%s", seq, var1, var2, var3);
Ensure that you have the right number of parameters to match each format specifier.
The example above creates tabular data which can be easily imported into a spreadsheet. The t
is the escape code for a tab character. I prefer using tabs rather than commas because the data themselves could contain commas.
t
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Could you please add your code the the question?
– Koby Douek
Jun 8 '17 at 3:48