Best answer

Get initials from text

  • 7 April 2020
  • 8 replies
  • 173 views

Userlevel 4
Badge

Does anyone know of a way to get the initials from text? 

For example, if I have the text: ‘Paul Minors Zapier Expert’ and I want to return PMZE, how would I do that? I figured you could use a bunch of formatter steps to split the text and return the first letter then all caps everything. But this feels like a long-winded work around. 

Does anyone have any other ideas?

icon

Best answer by ikbelkirasan 12 May 2020, 01:26

@alex This would work for anything like “AlphaTester Inc”.

const companyPO = inputData.companyName
.split(/([A-Z])/)
.filter((w) => w.length === 1)
.join("");
output = [{ companyPO }];

 

View original

This topic has been closed for comments

8 replies

Userlevel 7
Badge +10

I bet @ikbelkirasan has some sort of code step that would help here.

Userlevel 7
Badge +11

Perhaps a Formatter step using Extract Pattern might be able to do the trick here? You’d need to find the Regex for extracting the first letter of each word, but a Google search might be able to help you with that one. 

Userlevel 7
Badge +11

Hi @paulminors - In a code step and assuming that you have an input property called “text”. The following code should return the initials from it.

const initials = inputData.text
.split(" ")
.map((word) => word && word[0].toUpperCase())
.join("");

output = [{ initials }];
Userlevel 4
Badge

Brilliant, thanks guys. I’ll let you know once I’ve tried this @ikbelkirasan 

Userlevel 7
Badge +1

Ay, we actually use the same exact thing to strip down a potential client to a PO Name of sorts. Here’s the code snippet that I have been using for a couple years:

var companyName = inputData.companyName;

var str = companyName;
var matches = str.match(/\b(\w)/g); // ['J','S','O','N']
var companyPO = matches.join(''); // JSON

output = [{companyPO}];

I’d love to add to it, so if the result is only 1 character, to instead use the first 4 characters of the name. For example:

Alpha Testing Corporation = ATC

Google = GOOG

or, a fun challenge is when a company is named something like:

AlphaTester Inc = ATI (taking the AT since they are both capital letters, even though they are the same word).

Any guidance on that would be super!

Userlevel 7
Badge +11

@alex This would work for anything like “AlphaTester Inc”.

const companyPO = inputData.companyName
.split(/([A-Z])/)
.filter((w) => w.length === 1)
.join("");
output = [{ companyPO }];

 

Userlevel 4
Badge

Brilliant, thank you both for your help on this!

Userlevel 7
Badge +1

Thanks @ikbelkirasan - worked perfectly 👌 using the split at any capital letter makes total sense.

I should be able to simply do the other piece of what I mentioned by taking what you did and adding a simple “if less than 1 character is returned, instead capitalize the first 4 letters and use that instead”.

Appreciate your response!