LoadRunner more useful String functions
********************************************************************************
Concatenation of strcat two strings.
1) strcat
char *strcat (char *to, const char *from);
function: link two strings.
Example: This example is using the Strcat link string: Cheers_lee and @hotmail. com
The script is as follows:
Char test[1024], *a = "@hotmail. com";
strcpy (Test, "Cheers_lee");
strcat (test, a);
Lr_output_message ("We can see%s", test);
After running, see the following statement in Executon log:
Starting action action.
ACTION.C (+): We can see[email protected]
********************************************************************************
STRCHR returns a pointer to the character in the first occurrence of the string.
2) STRCHR
Char *strchr (const char *string, int c);
function: Returns the string following the specified character in the string.
Example: This example is to return the first occurrence of an e character after all characters, and the last occurrence of the e character after all characters.
The script is as follows:
Char *string = "Cheers is a tester";
Char *first_e, *last_e;
First_e = (char *) STRCHR (string, ' E ');
Lr_output_message ("We can see the first occurrence of E:%s", first_e);
Last_e = (char *) STRRCHR (string, ' E ');
Lr_output_message ("We can see the last occurrence of E:%s", last_e);
After running, see the following statement in Executon log:
Starting action action.
ACTION.C: We can see the first occurrence of e:eers is a tester
ACTION.C: We can see the last occurrence of e:er
********************************************************************************
STRCMP compares two strings to determine the alphabetical order.
strcmp&stricmp
int strcmp (const char *string1, const char *string2), case sensitive.
int stricmp (const char *string1, const char *string2), and case insensitive.
function: compare strings.
Example: compares two strings by case-sensitive, and prints out their size relationships. The
script is as follows:
int result;
Char tmp[20];
Char string1[] = "We can see the string:cheers";
Char string2[] = "We can see the string:cheers";
result = strcmp (string1, string2);
if (Result > 0)
strcpy (TMP, "greater than");
else if (Result < 0)
strcpy (tmp, "less than");
else
strcpy (tmp, "equals");
Lr_output_message ("Strcmp:string 1%s String 2", TMP);
result = stricmp (string1, string2);
if (Result > 0)
strcpy (TMP, "greater than");
else if (Result < 0)
strcpy (tmp, "less than");
else
strcpy (tmp, "equals");
Lr_output_message ("Stricmp:string 1%s String 2", TMP); After the
runs, you see the following statement in Executon log:
Starting action action.
ACTION.C: strcmp:string 1 is less than String 2
Action.c: stricmp:string 1 equals string 2
********************************************************************************
strcpy copy a string to another place.
strcpy
Char *strcpy (char *dest, const char *source);
function: copies a string into another string.
Example: copy a string into a character array and print it out.
The script is as follows:
Char test[1024];
strcpy (Test, "What can we see?");
Lr_output_message ("%s", test);
After running, see the following statement in Executon log:
Starting action action.
ACTION.C (Ten): What can we see?
********************************************************************************
StrDup repeats a string.
strdup& strlwr
Char *strdup (const char *string);
function: copies a string.
Char *strlwr (char *string);
function: convert to lowercase letters.
Example: In this example, the group name of VUser is converted to lowercase letters. However, Lr_whoami returns the group name as a static buffer. Such a buffer cannot be manipulated. Copy this static buffer if there is a need to do so.
The script is as follows:
int id;
Char *groupname_static, *groupname;
Lr_whoami (&id, &groupname_static, NULL);
Lr_output_message ("groupname=%s", groupname_static);
GroupName = (char *) strdup (groupname_static);
GroupName = (char *) strlwr (groupname);
Lr_output_message ("Lower case groupname=%s", groupname);
Free (groupname);
The above script is saved with Vugen as: change
Run in controller (set to always send messages)
After running, see the following statement in log:
Starting action action. [msgid:mmsg-15919]
ACTION.C (one): Groupname=change [msgid:mmsg-17999]
ACTION.C (+): lower case Groupname=change [msgid:mmsg-17999]
********************************************************************************
STRICMP performs a case-sensitive comparison of two strings.
********************************************************************************
Strlen returns the length of a string.
Strlen
size_t strlen (const char *string);
function: returns the string length (bytes).
Example: This is a simple example of getting the number of characters in a string. and print it out.
The script is as follows:
Starting action action. [msgid:mmsg-15919]
ACTION.C (one): Groupname=change [msgid:mmsg-17999]
ACTION.C (+): lower case Groupname=change [msgid:mmsg-17999]
After running, see the following statement in log:
ACTION.C (): The sentence has letters
********************************************************************************
STRLWR converts the string to lowercase.
********************************************************************************
strncat function concatenation? From one string to another character.
Strncat
Char *strncat (char *to_string, const char *from_string, size_t N);
function: concatenate a string behind another string.
Example: here, I write two random strings, use this function to connect them, and print them out.
The script is as follows:
Char str1[]= "Cheers is";
Char str2[]= "a tester.";
Lr_output_message ("What can we see?");
Lr_output_message ("The str1 is%s.", str1);
Strncat (str1,str2,20);
Lr_output_message ("The str1 is%s.", str1);
After running, see the following statement in log:
ACTION.C (9): What can we see?
ACTION.C: The str1 is Cheers.
ACTION.C: The str1 is Cheers is a tester.
Note: We can see that there is no connection before the str1: Cheers is, the concatenated string is: Zee is a tester. You can also look at the strcat function.
********************************************************************************
STRNCMP compares the first n characters of a two string.
8) strncmp
int strncmp (const char *string1, const char *string2, size_t N);
function: compares the top n bits of two strings.
Example: compare two strings and print out the results of the comparison. Here I write with the strcmp above.
The script is as follows:
char result;
Char str1[]= "Cheers is a tester.";
Char str2[]= "Cheers is a tester.";
Char str3[]= "Cheers is a tester?";
result = strcmp (STR1,STR2);
if (Result > 0)
Lr_output_message ("STR1 is greater than str2.");
else if (Result < 0)
Lr_output_message ("str1 is less than str2.");
Else
Lr_output_message ("str1 is equal to str2.");
result = strncmp (str1, STR3, 30);
if (Result > 0)
Lr_output_message ("STR1 is greater than STR3.");
else if (Result < 0)
Lr_output_message ("str1 is less than STR3.");
Else
Lr_output_message ("str1 is equal to STR3.");
After running, see the following statement in log:
Starting Iteration 1.
Starting action action.
ACTION.C: str1 is equal to str2.
ACTION.C: STR1 is less than STR3.
********************************************************************************
strncpy copies the first n characters of a string to another place.
********************************************************************************
STRNICMP executes a case-sensitive comparison of n strings.
********************************************************************************
STRRCHR finds the last occurrence of a character in a string.
********************************************************************************
Strset fills a string with a specific character.
********************************************************************************
STRSPN returns the length of a leading character in a string that is contained in a specified string.
********************************************************************************
STRSTR returns a string that appears for the first time in another
********************************************************************************
LoadRunner more useful String functions