Highlighted
Absent Member.
Absent Member.
13257 views

String comparison function in LR

Hi,

Thanks in advance,

Can any one help on string comparison function otherthan strcmp to use in Load Runner scripting.

I need to compare a string with the value captured and saved in LR variable.

For Example :  result=strcmp((lr_eval_string("{c_planGroupVal}"),"DC")

Please suggest me with an alternate string comparison function which can be used in LR script.

Regards

0 Likes
2 Replies
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: String comparison function in LR

Greetings,

LoadRunner use C commands and it can be programmatic solved by using the different C language commands and logic.

Following next you have an example about how compare strings using the command strnicmp.

 

Example: strnicmp

The following example compares two strings, string1 and string2, which are nearly identical - string2 contains the word "one" instead of "the" and the word "dog" in uppercase instead of lowercase.

stricmp returns an unequal comparison because it compares the whole string which includes the word imbalance "one" and "the". strnicmp compares 30 characters only, which falls short of the word imbalance in the strings. strnicmp also ignores the uppercase "DOG". It therefore returns an equal comparison.

    int result;
    char tmp[20];
    char string1[] = "The quick brown dog jumps over the lazy fox";
    char string2[] = "The quick brown DOG jumps over one lazy fox";
// Perform a case sensitive comparison 
    result = stricmp( string1, string2 ); 
    if (result > 0)
        strcpy(tmp, "greater than");
    else if (result < 0)
        strcpy(tmp, "less than");
    else
        strcpy(tmp, "equal to");
    lr_output_message ("stricmp: String 1 is %s string 2", tmp);
// Compare 30 chars  
    result = strnicmp( string1, string2 , 30); 
    if (result > 0 )
        strcpy( tmp, "greater than");
    else if (result < 0)
        strcpy(tmp, "less than"); 
    else
        strcpy(tmp, "equal to");
    lr_output_message ("strnicmp: String 1 is %s string 2", tmp);
Output:
Action.c(17): stricmp: String 1 is greater than string 2
Action.c(28): strnicmp: String 1 is equal to string 2
0 Likes
Highlighted
Super Contributor.
Super Contributor.

Re: String comparison function in LR

Hello there, you didn't tell what property of strcmp you didn't like.

Does it help you to look on strcmp() like working just like substraction? Comparing two numbers, using substraction and zero, you get operations which resemble the usage of strcmp, as you can see here:

5 - 4 > 0   (while strcmp("ccc", "bbb") > 0) i.e., first argument is larger than the second

4 - 4 = 0 (while strcmp("bbb", "bbb") = 0) i.e., both arguments are equal

3 - 4 < 0 (while strcmp("aaa", "bbb") < 0) i.e., first argument is less than the second

 

As you notice, you get a positive, zero or negative result in both cases. Also, don't forget functions like strlen("some string") or strstr("haystack with needle and thread", "needle") to see if you have a string at all or if the second string is part of the first..

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.