LoadRunner字符串处理 - 补齐字符串

本文介绍了一种在LoadRunner中实现字符串前补零的方法,通过编写C函数lr_padstr来实现在字符串前添加特定字符以达到指定长度的目的。文章提供了完整的代码示例并展示了如何在LoadRunner脚本中调用此函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有些时候需要在某个字符串的前面用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值