在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给我们提供了一个很强大的数据处理功能-查找和替换,通过这个功能,我们可以快速地找到想要 ...
随机推荐
- 循序渐进PYTHON3(十三) --8-- DJANGO之ADMIN
admin简单使用: 1.urls.py 2.settings.py 3.models.py from django.db import models classUserInfo(models ...
- Linux修改用户基本信息(不含密码)
如果想修改密码请查看Linux命令之passwd.chpasswd (1).使用usermod修改用户基本信息 Linux命令之usermod (2).进入配置文件修改用户信息 使用vim /etc/ ...
- Python的替换函数——strip(),replace()和re.sub()(转)
原文地址:http://blog.csdn.net/zcmlimi/article/details/47709049 在Python中常用的三个"替换"函数是strip(),rep ...
- 【BZOJ 1049】 1049: [HAOI2006]数字序列 (LIS+动态规划)
1049: [HAOI2006]数字序列 Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变 ...
- WSDL格式浅析
其中,WSDL是一种 XML 格式,用于将网络服务描述为一组端点,这些端点对包含面向文档信息或面向过程信息的消息进行操作.这种格式首先对操作和消息进行抽象描述,然后将其绑定到具体的网络协议和消息格式上 ...
- WQS二分题集
WQS二分,一种优化一类特殊DP的方法. 很多最优化问题都是形如“一堆物品,取与不取之间有限制.现在规定只取k个,最大/小化总收益”. 这类问题最自然的想法是:设f[i][j]表示前i个取j个的最大收 ...
- [UOJ336]无限之环
题目的要求就是每个接头都有且仅有一个与其相连的接头,所以不妨给每个接头$1$的流量,对整个网格图黑白染色后(源点$\mathop\rightarrow\limits^\infty$黑点,白点$\mat ...
- 【贪心】Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A. String Reconstruction
在每个给出的子串的起始位置打个标记,记录的是从这里开始的最长子串. 然后输出的时候就扫,如果遇到开始位置,就从这里开始输出,如果其后被更长的覆盖,就跳转到更长的串进行输出. 如果位置没被覆盖,就输出' ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学
C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...
- HDU 5626 Clarke and points 平面两点曼哈顿最远距离
Clarke and points 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5626 Description Clarke is a patie ...