The code above needed some enhancements and tweaking for working right.
I am putting the modified code here for any who might need it 🙂
charNum = 1; //The index of word which you want to take
myLayer=thisComp.layer("Change Text Here");
totChars=myLayer.text.sourceText.value.length; //Total Number of characters in the sentence (excluding \r)
totWords=getWords(myLayer); //Number of words there are in the sentence
//defining other variables
cont=true;
count=0;
i=0;
j=0;
fJ=0;
txt=[];
while(cont && i=charNum)
{
cont=false;
break;
}
}
txt[j]=myLayer.text.sourceText[i];
i++;
j++;
}
fJ;
txt.splice(fJ, (txt.length-fJ));
txt.join("");
function getWords(layer)
{
x=0;
y=0;
while (x<totChars)
{
if (layer.text.sourceText[x]=="\r")
{y++;}
x++;
}
return y+1;
}