Page 1 of 1

How can I get the number of word in a text layer?

Posted: April 18th, 2017, 11:56 pm
by wheelz13
Hey there,

Currently, I can count characters with this:

Code: Select all

myText = text.sourceText;
myText.length
What I really need is a way to count the number of words within a text layer.

Is this possible?

Thanks in advance! :-)

Re: How can I get the number of word in a text layer?

Posted: April 19th, 2017, 5:09 am
by runegan
There is probably a more accurate way to to this is, but this will be accurate most of the time:

Code: Select all

function getNumWords( string ) {

  // Split the string where there is spaces, then count the array length
  return string.split( ' ' ).length
}

Re: How can I get the number of word in a text layer?

Posted: April 19th, 2017, 2:19 pm
by wheelz13
Thanks!