loadrunner12.55:常用函数汇总说明之String Manipulation Functions参数函数

根据使用手册内容可见,LR支持的关于String ManipulationFunctions包括:

1、strcat   &  strncat  &  strcpy

strcat_strncat_strcpy_Func()
{
	char src[50], dest[50];
	
    char fullpath[1024], * filename = "logfile.txt";
    strcpy(fullpath, "c:\tmp");
    strcat(fullpath, "\");
    strcat(fullpath, filename);
    lr_output_message ("Full path of file is %s", fullpath);

	strcpy(src,  "This is source");
	strcpy(dest, "This is destination");
	strncat(dest, src, 11);
	lr_output_message ("the dest string  is %s", dest);
    
	return 0;
}

 2、strchr  &  strrchr

strchr_strrchr_Func()
{
	char * mystring = "His Excellency the Duke of Exeter";
    char * first_x, * last_x;
    
    first_x = (char *)strchr(mystring, 'x');
    lr_output_message ("The first occurrence of x: %s", first_x);
    
    last_x = (char *)strrchr(mystring, 'x');
    lr_output_message ("The last occurrence of x: %s", last_x);
    
    
	return 0;
}

  

 3、strcmp  &  stricmp

strcmp_stricmp_Func()
{
	int result;
    char tmp[20];
    char string1[] = "The quick brown dog jumps over the lazy fox";
    char string2[] = "The QUICK brown dog jumps over the lazy fox";

    // Case-sensitive comparison区分大小写
    result = strcmp( 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 ("strcmp: String 1 is %s string 2", tmp);
    
	// Case-insensitive 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 );

	return 0;
}

  

 4、strncmp

strncmp_Func()
{
	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 = strcmp(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 ("strcmp: String 1 is %s string 2", tmp );
// Compare 30 chars 
    result = strncmp( 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 ("strncmp: String 1 is %s string 2", tmp);

	return 0;
}

  

 5、strnicmp

strnicmp_Func()
{
	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);

	return 0;
}

  

 6、strlen   &  strncpy

strlen_strncpy_Func()
{
	int id, length;
    char tmp[1024];
// 8 characters + period + 3 characters + null 
    char filename[13]; 
 /*下面的strncpy示例创建的文件名符合8.3 DOS文件名格式,即,名称不得超过8个字符,文件后缀的长度必须为3个字符。
创建的文件名格式为[Vuser_id] _apptest.log。 在VuGen中运行示例时,lr_whoami返回的Vuser ID为-1,长度为两个字符。 
其余字符“ _apptest”构成的字符超过了所需的8个字符。 strncpy确保仅将8个字符复制到文件名。 最终名称为“ -1_appte.log”。*/
 
    lr_whoami (&id, NULL, NULL);//返回有关执行脚本的Vuser的信息。
    
    sprintf(tmp, "%d_apptest", id);
    length = strlen(tmp);
    lr_output_message ("name=%s length=%d", tmp, length);
// Copy 8 characters only

	lr_output_message("filename=%s", filename);
	
	strncpy(filename, tmp, 8);
	filename[8]='';//这个很重要,手动在数组末尾添加,实现安全使用strncpy函数,避免不确定性
    lr_output_message ("filename without suffix=%s", filename);
    strcat(filename, ".log");
    lr_output_message ("final filename=%s", filename);

	return 0;
}

    

 7、strstr

strstr_Func()
{
	int offset;
    char * position;
    char * str = "The quick brown dog jumps over the lazy fox";
    char * search_str = "dog";
    position = (char *)strstr(str, search_str);
    // strstr has returned the address. Now calculate * the offset from the beginning of str 
    offset = (int)(position - str + 1);
    lr_output_message ("The string "%s" was found at position %d", search_str, offset);

	return 0;
}

  

 8、strdup

strdup_Func()
{
	int id;
    char * groupname_static, * groupname;
/*在此示例中,Vuser的组名将转换为小写。 但是,lr_whoami将组名作为静态缓冲区返回,并且无法使用此类缓冲区。 如果需要操纵,则必须创建静态缓冲区的副本。
strdup创建静态缓冲区groupname_static的副本。 然后将新缓冲区(组名)中的字符转换为小写。*/    
    
    // Get the group name from VuGen 
    lr_whoami (&id, &groupname_static, NULL);
    lr_output_message ("groupname=%s", groupname_static);
    // Make a copy of groupname_static so we can change it
    groupname = (char *)strdup(groupname_static);
    groupname = (char *)strlwr(groupname);//将字符串参数groupname转换为小写形式
    lr_output_message ("lower case groupname=%s", groupname);
    free(groupname);

	return 0;
}

Vugen回放输出:

 Controller执行输出:

 9、strspn

 

strspn_Func()
{
	char * str = "corroborative";
    int rc;

    if ((rc = strspn(str, "ro")) == 0)
    	
        lr_output_message ("No o's or r's found");
    else
        lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));
    

    if ((rc = strspn(str + 1, "ro")) == 0)//从orroborative检索
        lr_output_message ("No o's or r's found");
    else
        lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));

	return 0;
}

  

 10、strtok

strtok_Func()
{
	char path[] = "c:\mercury\lrun\bin\wlrun.exe";
    char separators[] = ".\"; 
    char * token;


	    /* strtok使用分隔符来查找token之间的中断。 此处的分隔符为“ ”或“.”。

当对同一字符串使用多次调用strtok时,仅在第一次调用时指定该字符串。 后续调用必须将NULL作为字符串参数传递,如下所示。 */
	    
    // Get the first token

    token = (char *)strtok(path, separators); 
    if (!token) {
        lr_output_message ("No tokens found in string!");
        return( -1 );
    }
	// While valid tokens are returned
    while (token != NULL ) { 
        lr_output_message ("%s", token );
	// Get the next token
        token = (char *)strtok(NULL, separators); 
    }

	return 0;
}

  

the end!

  

原文地址:https://www.cnblogs.com/apple2016/p/14693086.html