参考《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;
}

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

在LoadRunner中查找和替换字符串相关推荐

  1. 在多行中查找和替换vim中的字符串

    本文翻译自:Find and replace strings in vim on multiple lines I can do :%s/<search_string>/<repla ...

  2. python字符串之查找与替换_在Python中查找和替换文本

    最简单的查找替换 在Python中查找和替换非常简单,如果当前对象是一个字符串str时,你可以使用该类型提供的find()或者index()方法查找指定的字符,如果能找到则会返回字符第一次出现的索引, ...

  3. 在Python中查找和替换文本,玩转Python正则

    最简单的查找替换 在Python中查找和替换非常简单,如果当前对象是一个字符串str时,你可以使用该类型提供的find() 或者index() 方法查找指定的字符,如果能找到则会返回字符第一次出现的索 ...

  4. Word中查找和替换功能详细介绍

    在Word中查找和替换功能很多人都会使用,最通常是用它来查找和替换文字,但实际上还可用查找和替换格式.段落标记.分页符和其他项目,并且还可以使用通配符和代码来扩展搜索. 一.查找和替换文字 例如:将& ...

  5. php 查找文件 替换内容,关于php:文件中查找和替换功能困扰

    我知道这不是编程问题吗? 但是我需要另一个有关在PHP代码编辑器中查找和替换功能的帮助. 我正在使用eclipse和Dreamweaver,我有大约650个php文件,并希望替换所有文件中的字符串而无 ...

  6. excel 替换 换行符_在Excel中查找和替换换行符

    excel 替换 换行符 When you want to create a line break (line feed) in a cell, you press Alt + Enter, to s ...

  7. html实现文本的查找与替换,在 InDesign 中查找并替换文本

    在对文档进行修订时,"查找并替换"操作将很有用.您可以全局查找并替换文本.对象.字体.字形和颜色. "查找/更改"对话框 A. 查找/更改选项卡 B. &quo ...

  8. Windows dos 在某个目录下的所有文件中查找包含某个字符串的文件

    findstr /s /i "str" *.* 上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"str"这个字符串.*.*表示所有类型的文件 ...

  9. JS实现文本中查找并替换字符

    JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head><style type=" ...

  10. 在Visual Studio Code中查找并​​替换为换行符

    本文翻译自:Find and replace with a newline in Visual Studio Code I am trying out the new Microsoft Visual ...

最新文章

  1. 关于微信的jsapi_ticket的获取方法;
  2. 科大星云诗社动态20210411
  3. 自我总结 (三) --(Java Web学习)
  4. Angular页面发生更新时,更新如何从view层传递到model层
  5. NET问答: 如何按属性进行 Distinct() ?
  6. 敏捷开发一千零一问系列之十五:同时实施CMMI和敏捷哪个为主?
  7. python教程循环语句,Python之新手教程4、循环语句
  8. GARFIELD@05-01-2005
  9. 2345浏览器网址_清理流氓网站2345.com劫持浏览器
  10. phpstom怎样导出数据库_详解mysql数据库sql优化技巧总结
  11. 基于python的图书管理系统设计与实现论文_图书管理系统的设计与实现图书管理系统论文...
  12. matlab倒立摆不同初始条件,倒立摆MATLAB建模
  13. Linux命令:ssh命令
  14. jmeter使用手册
  15. 关于在PLSQL中实现DEBUG调试功能的方法
  16. 软件外包公司到底干啥的?要不要去外包公司?
  17. 电脑如何去除视频水印?
  18. 系统没有安装任何软件如何查看电脑开机启动项
  19. java图片变成黑白代码_转:Java对图片的处理---缩放图像、图像切割、图像类型转换、彩色转为黑白...
  20. 闲鱼SPU体系构建的背后

热门文章

  1. SharePoint 2007部署过程详细图解(上)— 前期环境搭建
  2. html标签和asp.net在GridView里面绑定js的区别
  3. 用R演示逻辑回归过程-值得收藏
  4. 个人生活的量化分析(三):考研英语初探
  5. Oracle PL SQL完全自学手册pdf
  6. Out of resources when opening file './xxx.MYD' (Errcode: 24)
  7. 从 global.console 看 Node.js 中的作用域
  8. 风险案例-24期-缺少严格的变更及版本控制流程,导致问题反复,调试工作量和时间增加...
  9. ERP系统容灾方案对ERP生产系统的影响
  10. 7、sudo权限问题考察一题(考试题答案系列)