在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;
}
在LoadRunner中查找和替换字符串的更多相关文章
- C# 在word中查找及替换文本
C# 在word中查找及替换文本 在处理word文档时,很多人都会用到查找和替换功能.尤其是在处理庞大的word文档的时候,Microsoft word的查找替换功能就变得尤为重要,它不仅能让我们轻易 ...
- 在stream流和byte[]中查找(搜索)指定字符串
在 stream流 和 byte[] 中查找(搜索)指定字符串 这里注重看的是两个 Search 的扩展方法,一个是 stream 类型的扩展,另一个是 byte[] 类型的扩展, 如果大家有更好的“ ...
- 在某个目录下的所有文件中查找包含某个字符串的Windows命令
findstr可以完成这个工作. 上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...
- JS实现文本中查找并替换字符
JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head> <style type=" ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- Ubuntu中的在文件中查找和替换命令
分类: 9.Linux技巧2009-09-29 13:40 1429人阅读 评论(0) 收藏 举报 ubuntujdbc 1.查找 find /home/guo/bin -name /*.txt | ...
- Shell:sed用法 - 查找并替换字符串
原文链接 语法 sed 's/serach_str/replace_str/g' file_path 在某个文件中查找所有的serach_str并替换为replace_str 参数 描述 serach ...
- Word 查找和替换字符串方法
因为项目需要通过word模板替换字符串 ,来让用户下载word, 就在网上找了找word查找替换字符串的库或方法,基本上不是收费,就是无实现,或者方法局限性太大 .docx 是通过xml来存储文字和其 ...
- C# 在excel中查找及替换数据
在使用Excel处理数据时,有时候工作表内容很多,如果手动地一行一行的找数据很难发现它们在哪个地方.微软Excel给我们提供了一个很强大的数据处理功能-查找和替换,通过这个功能,我们可以快速地找到想要 ...
随机推荐
- webpack配置点记录
jsx语法需要用到babel-preset-react: 通用babel-loader相关如下: 以上配置还只能针对ES6情况,static defaultProps是ES7的特性,需要引入新的配置:
- [转载]开发 Spring 自定义视图和视图解析器
原文出处 http://www.ibm.com/developerworks/cn/java/j-lo-springview/ 概述 Spring 3.0 默认包含了多种视图和视图解析器,比如 JSP ...
- 判断数独是否合法(LintCode)
判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填 ...
- Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)
Pipeline as Code是2.0的精髓所在,是帮助Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)华丽转身的关键推手.所谓 ...
- 多协议注入工具t50
多协议注入工具t50 t50是Kali Linux自带的一款网络数据包注入工具.该工具支持15种协议,不仅涵盖常规协议(ICMP.TCP.UDP),还涵盖基础协议和路由协议(GRE.IPSec.R ...
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...
- 鹦鹉学舌1——C语言初学者百题大战之三
#include<stdio.h> int main() { int a; scanf("%d",&a); printf("%d",a-a+ ...
- fedora19/opensuse13.1 配置openvpn client
Date: 20140207Auth: Jin 1.install # yum -y install openvpn #zypper install openvpn 2.copy user key # ...
- GridControl事件
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...