Extracting characters from text (limited)

This was in response to @Ronnie’s comment (below) on this post about calculations.

At first, I was going to respond to the initial post. It quickly grew too complex for that post’s requirement for “easy to set up” calculations. So, I figured it seemed better here and the community could benefit from it.

Separate questions for first and last name

Suppose we have:

  • 2 questions that ask for a respondent’s first and last name
  • first — custom pre-fill key for the first name
  • last — custom pre-fill key for the last name

To grab the first letter of their first name and the first 3 letters of their last name, your calculation will look similar to:

first_slice = LEFT({{ first }}, 1);
second_slice = LEFT({{ last }}, 3);

referral_tag = first_slice || second_slice;

IF(AND({{ first }}, {{ last }}), referral_tag, "");

Also, this will grab exactly what the respondent enters. So, if you’re looking for only lowercase or only uppercase characters in your tag, you can wrap the last statement in LOWER() or UPPER().

LOWER(IF(AND({{ first }}, {{ last }}), referral_code, ""));
UPPER(IF(AND({{ first }}, {{ last }}), referral_code, ""));

One question for first and last name together

Alternatively, you may have the full name in one answer. Let’s assume the custom pre-fill key for that is name. For the sake of simplicity and my own sanity (I’ll figure out why GET() and SLICE() hate me later), let’s assume that the respondent enters a first and last name only. No middle name or suffix.

Here’s how we’d do that in a calculation:

split_name = SPLIT({{ name }}, ' ');

first_name = FIRST(split_name);
last_name = LAST(split_name);

first_slice = LEFT(first_name, 1);
second_slice = LEFT(last_name, 3);

referral_tag = first_slice || second_slice;

IF(AND(first_name != last_name, LEN(last_name) > 0), referral_tag, "");

First, we split the name that was entered into a list of values (an array) where each element was separated by a space in the name. Then, we grab the first and last value in that list and stick them in variables for first and last name, respectively. If you have a first and last name in one string, the first value would be the first name and the last value would be the last name.

Now that we’ve got our names separated like we did in the simpler example previously, we can perform the same operations to extract the first character from the first name and the first 3 characters from the last name.

Protecting against possible errors

Checking whether we have garbage values is a bit trickier, though. Since something like first_slice references first_name which then references an item in the list split_name, we want to make sure we’ve actually got something usable.

So, we check whether the first name is equal to the last name. Since we used FIRST() and LAST(), if there’s only one item in the list, like if you only entered your first name, it would be the first and last item. We want to protect against that, so we make sure the first and last values are not equal.

Then, we also make sure that the length of the last name is greater than 0. It seems obvious, but if you don’t include that check, it’ll create a second item in split_name if all you do is enter a space. This will give you a value of null. If the length is greater than 0, though, it’ll mean at least one character was entered after the space.

In closing

Phew, that was a lot! This is a bit more complex than typical operations in Paperform since you have to do it by hand, but it’s good to know and identical or similar methods can be applied to other complex operations that can be achieved through calculations.

I’d love to be able to do this with more arbitrary requirements, but, as indicated earlier, some of the functions are not playing nice with me. I’ll figure it out someday, though.

3 Likes

1 Like

My brain hurts, but I love that calculation and use it daily. I also use their tag to pipe into a one-touch Twitter share that goes in their email. When they click the link, it automatically opens their Twitter with a tweet, hashtags, and their referral link written out.

The HTML looks something like this

< a href="https://twitter.com/share?url=**https://yourwebsite.com/tellusyourstory**&text=People are more than just numbers and statistics. Let's elevate and amplify the impact %23covid19 is having on individuals across the world. Tell us your story. Please RT! &#128591 %23askpatients %23stayhome %23highriskcovid19 %23COVID-19;via=**your company twitter handle name without the @**" rel="noopener noreferrer" target="_blank"> Click to share on Twitter

7 Likes