在LoadRunner中查詢和替換字串
參考《Search & Replace function for LoadRunner》:
http://ptfrontline.wordpress.com/2009/03/13/search-replace-function-for-lr/
LoadRunner中沒有直接的函式支援查詢並替換字串,因此可以封裝一個lr_replace函式出來:
// ----------------------------------------------------------------------------
//
// Description:
// Search for and replace text within a string.
//
// Parameters:
// src (in) - pointer to source string
// from (in) - pointer to search text
// to (in) - pointer to replacement text
//
// Returns:
// Returns a pointer to dynamically-allocated memory containing string
// with occurences of the text pointed to by 'from' replaced by with
// the text pointed to by 'to'.
//
// Notes:
// Do not use this directly in scripts unless you are a more advanced
// user who knows C and string handling. See below for the function you
// should use!
//
// ----------------------------------------------------------------------------
char *strReplace(const char *src, const char *from, const char *to)
{
char *value;
char *dst;
char *match;
int size;
int fromlen;
int tolen;
// Find out the lengths of the source string, text to replace, and
// the replacement text.
size = strlen(src) + 1;
fromlen = strlen(from);
tolen = strlen(to);
// Allocate the first chunk with enough for the original string.
value = (char *)malloc(size);
// We need to return 'value', so let's make a copy to mess around with.
dst = value;
// Before we begin, let's see if malloc was successful.
if ( value != NULL )
{
// Loop until no matches are found.
for ( ;; )
{
// Try to find the search text.
match = (char *) strstr(src, from);
if ( match != NULL )
{
// Found search text at location 'match'.
// Find out how many characters to copy up to the 'match'.
size_t count = match - src;
// We are going to realloc, and for that we will need a
// temporary pointer for safe usage.
char *temp;
// Calculate the total size the string will be after the
// replacement is performed.
size += tolen - fromlen;
// Attempt to realloc memory for the new size.
//
// temp = realloc(value, size);
temp = (char *)realloc(value, size);
if ( temp == NULL )
{
// Attempt to realloc failed. Free the previously malloc'd
// memory and return with our tail between our legs.
free(value);
return NULL;
}
// The call to realloc was successful. But we'll want to
// return 'value' eventually, so let's point it to the memory
// that we are now working with. And let's not forget to point
// to the right location in the destination as well.
dst = temp + (dst - value);
value = temp;
// Copy from the source to the point where we matched. Then
// move the source pointer ahead by the amount we copied. And
// move the destination pointer ahead by the same amount.
memmove(dst, src, count);
src += count;
dst += count;
// Now copy in the replacement text 'to' at the position of
// the match. Adjust the source pointer by the text we replaced.
// Adjust the destination pointer by the amount of replacement
// text.
memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else // No match found.
{
// Copy any remaining part of the string. This includes the null
// termination character.
strcpy(dst, src);
break;
}
} // For Loop()
}
return value;
}
// ----------------------------------------------------------------------------
//
// Description:
// Find and replace text within a LoadRunner string.
//
// Parameters:
// lrparam (in) - pointer to LoadRunner Parameter Name
// findstr (in) - pointer to text top search for
// replacestr (in) - pointer to text to use as replacement
//
// Returns:
// Returns an integer. 0=Error, 1=Success.
//
// Example:
// lr_save_string( "This is a small test of the search and replace function", "LRParam");
// lr_replace( "LRParam", "a", "-x-" );
// lr_output_message( "%s", lr_eval_string("{LRParam}") );
//
// ----------------------------------------------------------------------------
int lr_replace( const char *lrparam, char *findstr, char *replacestr )
{
int res = 0;
char *result_str;
char lrp[1024];
// Finalize the LR Param Name
sprintf( lrp, "{%s}", lrparam);
// Do the Search and Replace
result_str = strReplace( lr_eval_string(lrp), findstr, replacestr );
// Process results
if (result_str != NULL )
{
lr_save_string( result_str, lrparam );
free( result_str );
res = 1;
}
return res;
} // EOF
在指令碼中引用包括上面程式碼的標頭檔案“lr_replace.h”,使用lr_replace函式的例子如下所示:
#include "lr_replace.h"
Action()
{
// Store a string into "MyPar" parameter
lr_save_string("This is a string", "MyPar");
// For examples sake, convert it to URL encoded format
web_convert_param( "MyPar",
"SourceEncoding=PLAIN",
"TargetEncoding=URL", LAST);
// Output the current result
lr_output_message("%s", lr_eval_string("{MyPar}"));
// Replace the ? characters with %20
lr_replace("MyPar", "+", "%20" );
// Output new result
lr_output_message("%s", lr_eval_string("{MyPar}"));
return 0;
}
相關文章
- Linuxvivim查詢和替換字串命令Linux字串
- Python字串string的查詢和替換Python字串
- 使用 sed 命令查詢和替換檔案中的字串的 16 個示例字串
- 使用sed 命令查詢和替換檔案中的字串的方法總結字串
- vim查詢替換
- 如何在word中進行查詢與替換 word文件中的替換與查詢功能
- PostgreSQL 查詢替換函式SQL函式
- js中字串的替換JS字串
- js中字串全部替換JS字串
- Find and Replace Pattern(C++查詢和替換模式)C++模式
- linux中批量替換文字中字串Linux字串
- 替換快捷鍵ctrl加什麼 word查詢和替換快捷鍵是什麼
- vim下多行查詢替換簡單命令
- D4.玩轉查詢與替換
- Linux vi替換字串Linux字串
- Python實用技法第24篇:正則:查詢和替換文字Python
- 批次word文件內容查詢替換的方法
- js replace替換字串,同時替換多個方法JS字串
- 替換字串中的空格《演算法很美》字串演算法
- Problem 4:替換空格(字串)字串
- Linux的VI (連線行,查詢和替換,多檔案編輯)Linux
- Golang 字串分割,替換和擷取 strings.SplitGolang字串
- 【Hive】字串替換函式translate和regexp_replaceHive字串函式
- js替換字串裡的空格JS字串
- grep sed 大批次替換字串字串
- 字串查詢(字串雜湊)字串
- python如何將字串中的所有"you"替換成"we"Python字串
- JavaScript 替換字串全部指定內容JavaScript字串
- 7-15 字串替換 (6分)字串
- JavaScript replace()替換字串中指定字元JavaScript字串字元
- 第五章 字串專題 ---------------- 5.4 實踐:替換字串中的空格字串
- WinForm使用DataGridView實現類似Excel表格的查詢替換ORMViewExcel
- python怎麼查詢字串中是否包含某個字串Python字串
- 在 with 查詢中只查詢個別欄位
- java字串%s格式化替換方法Java字串
- 正規表示式的字串替換方法字串
- SQLserver2008批次替換字串SQLServer字串
- SQL語句替換查詢結果的的寫法舉例SQL
- linux下查詢字串Linux字串