Back to Blog

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.

Back to Blog