有些时候需要在某个字符串的前面用0补齐,以便满足长度的格式要求。

在LoadRunner中可以封装出一个函数来处理这种问题:

/*

Function to pad a string to x characters adding the required character at the start of the string

(Note string length limited by length of the array)

Author: Steven Woodcock, Sopra Group

Inputs:

strCharToPad - The String that requires to be padded out i.e. "123456"

cParamName - The name of the parameter you want the end value to be saved to

iValLength - The length you want the string to be padded out to i.e. 9

cValueToAdd - The character you want to pad the string out with, must be a single character i.e. "0"

Outputs:

Creates a LoadRunner parameter of a name specified in the input 'cParamName'

*/

int lr_padstr(char* cCharToPad, char* cParamName, int iValLength, char* cValueToAdd){

char cTemp[1024] = "";

int iLoop = 0;

int iMainLoop = 0;

int iSubLoop = 0;

for(iMainLoop = 0; iMainLoop < iValLength; iMainLoop++){

if (iMainLoop == strlen(cCharToPad)) {

iLoop = iValLength - iMainLoop;

for (iSubLoop = 0; iSubLoop < iLoop; iSubLoop++) {

strcat(cTemp, cValueToAdd);

}

}

}

strcat(cTemp, cCharToPad);

lr_save_string(cTemp, cParamName);

}

lr_padstr可以在指定字符串前补齐指定长度的某个字符,然后把修改后的字符串存入LoadRunner参数中。把以上代码放到lr_padstr.h头文件中,在LoadRunner脚本中引用:

#include "lr_padstr.h"

Action()

{

//     For the lr_padstr function

char * cShortValue = "123";

//     For the sprintf function

char cPaddedValue[8] ="";

int iShortValue = 123;

int i;

//     The lr_padstr function, written by Steven Woodcock can be used to pad with zeroes

lr_padstr(cShortValue,"pPaddedParameter",8,"0");

lr_output_message("Padded value is %s", lr_eval_string("{pPaddedParameter}"));

//     The PadToX function can also be used to pad with other characters (e.g. x)

lr_padstr(cShortValue,"pPaddedParameter",8,"x");

lr_output_message("Padded value is %s", lr_eval_string("{pPaddedParameter}"));

return 0;

}

参考:

http://www.bish.co.uk/~richardmjbishop/index.php?option=com_content&view=article&id=85%3Asample-loadrunner-script-pad-a-string-with-leading-zeroes&catid=34%3Arecent&Itemid=1

LoadRunner字符串处理 - 补齐字符串的更多相关文章

  1. C# 实现数字字符串左补齐0的两种方法

    ); MessageBox.Show(sss); return; 代码如上,自动补齐前面的0

  2. java中字符串左右补齐【转】

    /** * 右左补齐 */ public static String padRight(String src, int len, char ch) { int diff = len - src.len ...

  3. sqlserver 2008 左补齐字符串

    SQLServer:right函数 语法 Right(string, length)   Right 函数的语法具有下面的命名参数:   部分 说明 string 必要参数.字符串表达式,从中最右边的 ...

  4. C#实现数字字符串左补齐0的方法

    如下: ; , '); //0003 (推荐) s = string.Format("{0:d4}", n); //0003 再如: ; 方法1:Console.WriteLine ...

  5. C#实现数字字符串左补齐0的3种方法

    int n = 3; string s = n.ToString().PadLeft(4, '0'); //0003 s = string.Format("{0:d4}", n); ...

  6. PHP数字字符串左侧补0、字符串填充和自动补齐的几种方法

    一.数字补0. 如果要自动生成学号,自动生成某某编号,就像这样的形式“d0000009”.“d0000027”时,那么就会面临一个问题,怎么把左边用0补齐成这样8位数的编码呢?我想到了两种方法实现这个 ...

  7. StringUtils字符串工具类左侧补齐(leftPad)、右侧补齐(rightPad)、左右两侧补齐(center)工具方法

    这里使用的是 org.apache.commons.lang.StringUtils;下面是StringUtils工具类中字符串左侧补齐的方法,示例如下: //左侧补齐 第一个参数:原始字符串,第二个 ...

  8. Delphi中字符串补齐方法

    函数功能:当Str不满Len长度时,在Str前自动填充PadStr以补足长度,例子如下: Str:原字符串 Len:补多长 PadStr:用什么补齐,比如‘0’ function PadString( ...

  9. C 一个字符串有三段,第一段原样输出,第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段

    C 一个字符串有三段,第一段原样输出.第二段为要输出字符串的长度,第三段为依据第二段长度补齐第一段 比如:输入abc 11 12.输出abc12121212 #include<stdio.h&g ...

随机推荐

  1. 洛谷P3216 [HNOI2011] 数学作业 [矩阵加速,数论]

    题目传送门 数学作业 题目描述 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N和 M,要求计算 Concatenate (1 .. N)Mod M 的值,其中 C ...

  2. 【BZOJ 3175】 3175: [Tjoi2013]攻击装置(二分图匹配)

    3175: [Tjoi2013]攻击装置 Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2) ...

  3. android viewpager fragment 优化 切换界面 延时加载

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 使用 碎片的 设置用户可见暗示visible hint  这个方法来做到. hint 是 ...

  4. [BZOJ 1500] 维护序列

    Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...

  5. BZOJ 1934 [Shoi2007]Vote 善意的投票(最小割)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1934 [题目大意] 每个人对于投票都有自己原来的观点:1或者0, 他可以违背自己原来的 ...

  6. BZOJ 1475 方格取数(二分图最大点权独立集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1475 [题目大意] 给出一个n*n的方格,从中取一些不相邻的数字,使得和最大 [题解] ...

  7. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  8. jsp ajax实例讲解

    下面介绍JSP前台表单内容通过Ajax异步提交到后台Servlet进行校验(校验方式多种,包括提取数据库信息,校验用户名是否重复等),异步在JSP表单页面显示校验结果信息的基本过程. 一.说明: 1. ...

  9. github之怎么上传本地项目

    github之怎么上传本地项目 以前都是在自己磁盘上的某个目录下,然后打开git bash,来进行把本地的一些文件推到远程github上. 之前的方法步骤: 1.在github上new一个库,然后gi ...

  10. 【转】记录PHP、MySQL在高并发场景下产生的一次事故

    看了一篇网友日志,感觉工作中值得借鉴,原文如下: 事故描述 在一次项目中,上线了一新功能之后,陆陆续续的有客服向我们反应,有用户的个别道具数量高达42亿,但是当时一直没有到证据表示这是,确实存在,并且 ...