标签:
globas.h中定义 //LoadRunner中没有直接的函数支持查找并替换字符串,因此可以封装一个lr_replace函数出来: // ---------------------------------------------------------------------------- char *strReplace(const char *src, const char *from, const char *to) { char *value; char *dst; char *match; int size; int fromlen; int tolen; size = strlen(src) + 1; fromlen = strlen(from); tolen = strlen(to); value = (char *)malloc(size); dst = value; if ( value != NULL ) { for ( ;; ) { match = (char *) strstr(src, from); if ( match != NULL ) { size_t count = match - src; char *temp; size += tolen - fromlen; temp = (char *)realloc(value, size); if ( temp == NULL ) { free(value); return NULL; } dst = temp + (dst - value); value = temp; memmove(dst, src, count); src += count; dst += count; memmove(dst, to, tolen); src += fromlen; dst += tolen; } else { strcpy(dst, src); break; } } } return value; } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- int lr_replace( const char *lrparam, char *findstr, char *replacestr ) { int res = 0; char *result_str; char lrp[1024]; sprintf( lrp, "{%s}", lrparam); result_str = strReplace( lr_eval_string(lrp), findstr, replacestr ); if (result_str != NULL ) { lr_save_string( result_str, lrparam ); free( result_str ); res = 1; } return res; }
在Action中定义:
login() { lr_save_string("This is a string", "MyPar"); //web_convert_param( "MyPar", // "SourceEncoding=PLAIN", // "TargetEncoding=URL", LAST); lr_output_message("%s", lr_eval_string("{MyPar}")); //lr_replace("MyPar", "+", "%20" ); //lr_output_message("%s", lr_eval_string("{MyPar}")); lr_replace("MyPar", "ing", "旺仔" ); //将Mypar中的ing字符串用旺仔替换 lr_output_message("%s", lr_eval_string("{MyPar}"));
标签:
原文地址:http://www.cnblogs.com/qmfsun/p/4575419.html