VuGen: Trim Strings With Dynamic Lengths On Left

Sometimes you need to strip off the first part of a string because there is some character and/or amount of crap at the end that you don’t need. The most obvious use of this is when you capture an e-mail address as a parameter and the string is like:

Let’s say all you need is the “giggity”, and not the rest. If you capture the exact same thing each time, you can just strip off anything after the 7th character. But what if the next time you run the script you capture:

You would be screwed because the length is different. Well, here is a quick way to use the “@” sign as a seperator. You can strip off anything up to and including the @ sign. This code looks through the string until it find the @ sign and remembers how many characters it took to reach it. Then it strips that character and the rest of it off.

---------------------Begin Script here-----------------------------
//Set up your variables.
// {VAR} represents whatever you had already as a parameter.
// I used a percent sign as my seperator because that was the
// character I wanted to start with when stripping the rest off the string
int iNumofChars;
char PreFix[5];
char *tempstring;
tempstring = lr_eval_string("{VAR}");
iNumofChars = strcspn(tempstring, "@");
strncpy(PreFix, tempstring, iNumofChars);
lr_save_string(PreFix, "pNewString");
// Now you should have a new parameter called {pNewString} with the junk stripped
//This includes the seperating character.
---------------------------End Script----------------------

Anyone want to get inventive and make this code shorter? How about not including the separating character? What if you want the last half of the string and not the first half? Please experiment and send me any cool modifications to this code if you create some.

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events