LoadRunner中常⽤的字符串操作函数LoadRunner中常⽤的字符串操作函数有:
strcpy(destination_string, source_string);
strcat(string_that_gets_appended, string_that_is_appended);51Testing软件测试⽹:J3~c:c[(w R%A2l              atoi(string_to_convert_to_int); //returns the integer value
itoa(integer_to_conver_to_string, destination_string, base); // base is 10
strcmp(string1, string2); // returns 0 if both strings are equal51Testing软件测试⽹q%US E K
对各函数的定义:
strcpy( ):拷贝⼀个字符串到另⼀个字符串中.
strcat( ):添加⼀个字符串到另⼀个字符串的末尾。
strcmp( ):⽐较两个字符串,如果相等返回0。
atoi():转换⼀个ASCII字符串为⼀个整型。
itoa():根据给定的进制,转换⼀个整型数据为ASCII字符串51Testing软件测试⽹D&V I2K D|
下⾯的例⼦使⽤了上⾯这些函数:
Actions()
{
char MyString1[20] = "";
char MyString2[20] = "";
char MyString3[20] = "Mercury2";
char Cstring[10] = "12345";
int Cint;
// MyString1 is empty
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
/
/ copy "Mercury1" into MyString1
//
strcpy(MyString1,"Mercury1");
// Now MyString1 contains "Mercury1"
//
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Copy MyString3 into MyString2
//
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
strcpy(MyString2,MyString3);
lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);
/
/ Catenate MyString2 to MyString1
//
strcat(MyString1,MyString2);
lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);
// Cstring is converted to integer Cint
//
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
Cint = atoi(Cstring);
lr_output_message(">>>>>>>>>> Cint = %d",Cint);
// Cint is converted to string
Cint = 100;
itoa(Cint,Cstring,10);
lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);
return0;
}
LoadRunner字符串⽐较的常见错误
最近在论坛上看到有⼈提问LoadRunner如何对两个字符串进⾏⽐较,其脚本中两个字符串进⾏⽐较结果总是不⼀样的。我把问题整理了⼀下以便注意这个容易被忽略的错误。脚本如下:
...
lr_save_string( "Hello World!","string1" );
lr_save_string( "Hello World!","string2" );
result = strcmp("string1","string2");
if ( result == 0 )
{
lr_output_message("the result is 0.");
}
else
{
lr_output_message("the result is not 0.");
}
⼤家可以看出脚本那⾥错了吗?
问题错在result = strcmp("string1","string2");这个上,这样变成了对字符串"string1"和"string2"的⽐较,⽽不是对变量的值进⾏⽐较,因此⽐较结果肯定是不⼀样的。
正确的写法有两种:
result = strcmp(&string1,&string2);
result = strcmp(lr_eval_string("{string1}"),lr_eval_string("{string2}"));
Loadrunner的字符串处理函数
1)strcat编辑本段回⽬录
  char *strcat ( char *to, const char *from );
  功能:链接两个字符串。
  例⼦:
  这个例⼦是⽤strcat链接字符串:Cheers_Lee和 @hotmail
  脚本如下:
char test[1024], *a = "@hotmail";
strcpy(test, "Cheers_Lee");
strcat(test, a);
lr_output_message("We can see %s",test);
  运⾏后在executon log中看到如下语句:
Starting action Action.
Action.c(16): We can see Cheers_Lee@hotmail 
2)strchr编辑本段回⽬录
  char *strchr ( const char *string, int c );
  功能:返回字符串中指定字符后⾯的字符串。
  例⼦:
  这个例⼦是返回第⼀个出现e字符以后所有的字符,和最后⼀次出现e字符以后所有的字符。  脚本如下:
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);
  运⾏后在executon log中看到如下语句:
Starting action Action.
Action.c(12): We can see the first occurrence of e: eers is a tester
Action.c(14): We can see the last occurrence of e: er   
3)Strcmp&stricmp编辑本段回⽬录
  int strcmp ( const char *string1, const char *string2 );⼤⼩写敏感。
  int stricmp ( const char *string1, const char *string2 );⼤⼩写不敏感。
  功能:⽐较字符串。
  例⼦:
  按是否区分⼤⼩写对⽐两个字符串,并打印出它们的⼤⼩关系。
  脚本如下:
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, "⼤于" );
else if( result < 0 )
strcpy( tmp, "⼩于" );
else
strcpy( tmp, "等于" );
lr_output_message( "strcmp: String 1 %s string 2", tmp );
result = stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "⼤于" );
else if( result < 0 )
strcpy( tmp, "⼩于" );
else
strcpy( tmp, "等于" );
lr_output_message( "stricmp: String 1 %s string 2", tmp );
  运⾏后在executon log中看到如下语句:
Starting action Action.
Action.c(22): strcmp: String 1 ⼩于 string 2
Action.c(33): stricmp: String 1 等于 string 2
4)strcpy编辑本段回⽬录
  char *strcpy ( char *dest, const char *source );
  功能:复制⼀个字符串到另⼀个字符串中。
  例⼦:
  复制⼀个字符串到字符数组中,并打印出来。
  脚本如下:
char test[1024];
strcpy(test, "what can we see?");
lr_output_message("%s", test);
  运⾏后在executon log中看到如下语句:
Starting action Action.
Action.c(10): what can we see?   
5)Strdup& strlwr编辑本段回⽬录
  char *strdup ( const char *string );
  功能:复制⼀个字符串。
  char *strlwr ( char *string );
  功能:转换成⼩写字母。
  例⼦:
  在这个例⼦中,Vuser的组名被转换为⼩写字母。但是lr_whoami把组名作为静态buffer返回。这样的buffer不能被操作。如果有操作需要,就复制这个静态buffer。  脚本如下:
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);
  上述脚本⽤vugen保存为:CHANGE
  在controller中运⾏(设置为总是发送消息)
  运⾏后在log中看到如下语句:
Starting action Action. [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change [MsgId: MMSG-17999] 
6)Strlen编辑本段回⽬录
  size_t strlen ( const char *string );
  功能:返回字符串长度(bytes).
  例⼦:
  这个例⼦很简单,就是得到⼀个字符串中的字符的个数。然后打印出来。
  脚本如下:
Starting action Action. [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change [MsgId: MMSG-17999]
  运⾏后在log中看到如下语句:
Action.c(13): The sentence has 18 letters
7)Strncat编辑本段回⽬录
  char *strncat ( char *to_string, const char *from_string, size_t n );
  功能:把⼀个字符串连接到另⼀个字符串后⾯。
  例⼦:
  在这⾥,我随便写了两个字符串,⽤此函数把他们连接起来,并打印出来。
  脚本如下:
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);
  运⾏后在log中看到如下语句:
Action.c(9): What can we see?
Action.c(10): The str1 is Cheers is .
Action.c(13): The str1 is Cheers is a tester..
  注:我们可以看到,没有连接前的str1是:Cheers is,连接后的字符串是:Zee is a tester。也可以看看strcat函数。 
8)strncmp编辑本段回⽬录
  int strncmp ( const char *string1, const char *string2, size_t n );
  功能:对⽐两个字符串的前n位。
  例⼦:
  对⽐两个字符串,并把对⽐结果打印出来。这⾥我和上⾯的strcmp⼀起写。
  脚本如下:
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.");
  运⾏后在log中看到如下语句:
Starting iteration 1.
Starting action Action.
Action.c(18): str1 is equal to str2.
Action.c(28): str1 is less than str3.
loadrunner⽐较有⽤的字符串函数
  strcat的串连两个字串。
  strchr返回指向第⼀次出现的字符串中的字符。
  STRCMP⽐较两个字符串来确定的字母顺序。
  STRCPY⼀个字符串复制到另⼀个地⽅。
  strdup重复⼀个字符串。
  stricmp执⾏区分⼤⼩写的⽐较两个字符串。
  strlen的返回⼀个字符串的长度。
  strlwr将字符串转换为⼩写。
  strncat函数串连?从⼀个字符串到另⼀个字符。
  STRNCMP⽐较两个字符串的前n个字符。
  strncpy⼀个字符串的前n个字符复制到另⼀个地⽅。
  strnicmp执⾏区分⼤⼩写的⽐较n个字符串。
  strrchr查的字符串中的字符的最后⼀次出现。
  strset⼀个特定的字符填充⼀个字符串。
  strspn返回⼀个指定的字符串中包含的字符串中的前导字符的长度。  strstr返回⼀个字符串第⼀次出现在另⼀个
把字符看成ASII的值 , 和数字⽐较⼤⼩⼀般,
if( strcmp(A,B) > 0 )  串A > 串B
if( strcmp(A,B) == 0 )  相同的串
if(strcmp(A,B) < 0 )  串A < 串B
int strcmp(char *str1, char *str2);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。