ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Split String Errors when using [1]

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Adam Haas
Split String Errors when using [1]
on Aug 8, 2018 at 10:20:23 pm

Here is my text:

Interviewing Verbal Communication
Interviewing Verbal Communication - Action Plan
Inexperienced % Describe a Conflict & Its Resolution
Inexperienced % Describe a Conflict & Its Resolution - Action Plan
Inexperienced % Describe a Conflict & Its Resolution # Español
Inexperienced % Describe a Conflict & Its Resolution # Español - Action Plan

I am displaying one line at a time like this:


TextArray = text.sourceText.split('\r');
ArrayIndex = timeToFrames()%TextArray.length;

TextArray[ArrayIndex];


That works great. Now, I have a second text layer called "SubText" that pulls just the first portion of this text. Here is my code:


ExperiencedOrInexperienced = thisComp.layer("LessonTextSource").text.sourceText.split(" % ")[0]

if (ExperiencedOrInexperienced == "Experienced")
"Experienced"
else if (ExperiencedOrInexperienced == "Inexperienced")
"Inexperienced"
else
"ASDF";


This works fine. However, when I try to use the rest of the text by changing the [0] into a [1], I get an error:


After Effects warning: Expression disabled. Error at line 1 in property 'Source Text'
of layer 1 ("CornerText') in comp 'TestComposition'.
property or method named '1' in Class 'Array' is missing or does not exist. It may
have been renamed, moved, deleted, or the name may have been mistyped.


I assume that this errors because their instance of text where there is no %, but why would it only error when I am trying to pull the second half of the text?


Return to posts index

Dan Ebberts
Re: Split String Errors when using [1]
on Aug 8, 2018 at 10:49:54 pm

split() creates an array and if the text doesn't have a "%" character, the array will only have one element (which will contain all the text of the line). When you try to access the 2nd element (which doesn't exist), you'll get that error. Your expression should check the length of TextArray before accessing the 2nd element.

Dan



Return to posts index

Adam Haas
Re: Split String Errors when using [1]
on Aug 13, 2018 at 9:38:53 pm

The code is throwing an error for instances where the % is not present, true. However, how will checking the length allow me to determine if there is a character in it?

I found this code:


myChar = "•";
if (text.sourceText[textIndex-1] == myChar) 100 else 0


that I tried to nest an IF statement within, but it's not working:


lessonTextSource = thisComp.layer("LessonTextSource").text.sourceText.replace(/\r?\n|\r/g,'');
percent = "%";

if (lessonTextSource[textIndex-1] == percent)
CornerText = lessonTextSource.split(" % ")[1];

if (ExperiencedOrInexperienced == "Experienced")
"Experienced"
else if (ExperiencedOrInexperienced == "Inexperienced")
"Inexperienced"
else
"ASDF"


I get this error:


After Effects warning: Expression disabled. Error at line 4 in property 'Source Text'
of layer 2 ('CornerText') in comp 'TestComposition'.
property or method named 'textIndex' in Class 'global' is missing or does
not exist. It may have been renamed, moved, deleted, or the name may have been mistyped.


Return to posts index


Dan Ebberts
Re: Split String Errors when using [1]
on Aug 13, 2018 at 9:43:19 pm

If the character isn't present, the length of the array will be 1 (and all of the text will be in array element [0]). If it is there, the length will be greater than one and the text will be split among multiple array elements.


Dan



Return to posts index

Adam Haas
Re: Split String Errors when using [1]
on Aug 13, 2018 at 10:48:15 pm

I was able to get this to work by adding the special characters, even if there is no text there, and then just displaying the innermost text :


TextArray = thisComp.layer("LessonTextSource").text.sourceText.split('\r');
ArrayIndex = timeToFrames()%TextArray.length;

TextArray[ArrayIndex].split("% ")[1].split(" -")[0];


I didn't think I could use split more than once, but it makes my body of text gross.

I came across someone using length, but didn't understand how it worked. Now I can't find where I saw it. Can you give me an example of how that works?


Return to posts index

Dan Ebberts
Re: Split String Errors when using [1]
on Aug 13, 2018 at 10:58:57 pm

Something like this maybe:


txt = "xxx%yyy";
txtArray = txt.split("%");
if (txtArray.length > 1)
txtArray[1]
else
txtArray[0]


Then try it without the % in the text.

Dan



Return to posts index


Adam Haas
Re: Split String Errors when using [1]
on Aug 13, 2018 at 11:32:05 pm

Oh that is so much better. Thank you so much for your patience.

Using my original text:

Interviewing Verbal Communication
Interviewing Verbal Communication - Action Plan
Inexperienced % Describe a Conflict & Its Resolution
Inexperienced % Describe a Conflict & Its Resolution - Action Plan
Inexperienced % Describe a Conflict & Its Resolution # Español
Inexperienced % Describe a Conflict & Its Resolution # Español - Action Plan

My code works as so:


HyphenText = thisComp.layer("LessonTextSource").text.sourceText.split(" - ");

if (HyphenText.length > 1)
HyphenText[1];
else
"";



PoundText = thisComp.layer("LessonTextSource").text.sourceText.split(" # ");

if (PoundText.length > 1)
PoundText[1];
else
"";



PercentText = thisComp.layer("LessonTextSource").text.sourceText.split(" % ");

if (PercentText.length > 1)
PercentText[0];
else
"";


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]