参考《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中查找和替换字符串的更多相关文章

  1. C# 在word中查找及替换文本

    C# 在word中查找及替换文本 在处理word文档时,很多人都会用到查找和替换功能.尤其是在处理庞大的word文档的时候,Microsoft word的查找替换功能就变得尤为重要,它不仅能让我们轻易 ...

  2. 在stream流和byte[]中查找(搜索)指定字符串

    在 stream流 和 byte[] 中查找(搜索)指定字符串 这里注重看的是两个 Search 的扩展方法,一个是 stream 类型的扩展,另一个是 byte[] 类型的扩展, 如果大家有更好的“ ...

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

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

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

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

  5. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  6. Ubuntu中的在文件中查找和替换命令

    分类: 9.Linux技巧2009-09-29 13:40 1429人阅读 评论(0) 收藏 举报 ubuntujdbc 1.查找 find /home/guo/bin -name /*.txt | ...

  7. Shell:sed用法 - 查找并替换字符串

    原文链接 语法 sed 's/serach_str/replace_str/g' file_path 在某个文件中查找所有的serach_str并替换为replace_str 参数 描述 serach ...

  8. Word 查找和替换字符串方法

    因为项目需要通过word模板替换字符串 ,来让用户下载word, 就在网上找了找word查找替换字符串的库或方法,基本上不是收费,就是无实现,或者方法局限性太大 .docx 是通过xml来存储文字和其 ...

  9. C# 在excel中查找及替换数据

    在使用Excel处理数据时,有时候工作表内容很多,如果手动地一行一行的找数据很难发现它们在哪个地方.微软Excel给我们提供了一个很强大的数据处理功能-查找和替换,通过这个功能,我们可以快速地找到想要 ...

随机推荐

  1. CodeForces 738D Sea Battle

    抽屉原理. 先统计最多有$sum$个船可以放,假设打了$sum-a$枪都没打中$a$个船中的任意一个,那么再打$1$枪必中. #pragma comment(linker, "/STACK: ...

  2. AndroidManifest.xml文件详解(uses-feature)

    http://blog.csdn.net/think_soft/article/details/7596796 语法(SYNTAX): <uses-featureandroid:name=&qu ...

  3. Web开发中的路径问题总结

    原文地址:http://zzqrj.iteye.com/blog/806909 路径问题在Web开发中算是令人比较蛋疼的问题,尤其是用相对地址时,同样的代码,在不同的目录结构中竟然会出现有对有错的结果 ...

  4. 线程间操作无效: 从不是创建控件“textBox2”的线程访问它

    如何:对 Windows 窗体控件进行线程安全调用 线程间操作无效: 从不是创建控件的线程访问它的三种方法 如果使用多线程处理来提高 Windows 窗体应用程序的性能,则你必须确保以线程安全的方式调 ...

  5. [POJ1980]Unit Fraction Partition(搜索)

    Unit Fraction Partition Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4571   Accepted ...

  6. 【Trie模板】HDU1251-统计难题

    [题意] n统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). [思路] 裸题,不过G++好像会超内存,C++就不会. #include<iostream> #include& ...

  7. 求数组的平均值 Exercise07_08

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求数组的平均值 * */ public class Exercise07_ ...

  8. PHP手册笔记

    <?php getenv — 获取一个环境变量的值 $ip = getenv ( 'REMOTE_ADDR' ); // 或简单仅使用全局变量($_SERVER 或 $_ENV) $ip = $ ...

  9. js 根据开始日期和结束日期显示倒计时

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  10. UDP和TCP的区别和联系

    UDP特点 将数据源和目的封装在数据包中,不需要简历连接 每个数据报的大小在限制在64K内 因无连接,是不可靠协议 不需要建立连接,速度快 TCP 建立连接.形成传输数据通道. 在连接中进行大量数据量 ...