Now let's take a look at advanced text methods and string manipulation.
A substring has three parts, the method name, substr, followed by two arguments
wrapped in parenthesis and separated by a comma and an optional space.
The first argument is the index location, measured in characters, that the substring will start.
The second argument is how many characters the substring will include, from its starting point.
Let's have a look at the Substrings text layer.
The name of the layer is 306A: Substrings.
Let's say you want to take the layer's name and output a modified version as the Source Text.
So first, you want to open the Source Text, I'll use U + U to open that up,
and I'm going to add a new expression.
So we're going to start simple and just use the name attribute.
So if you click away from the expression, you'll see that the text layer
now reflects the layer's name.
Now, the name attribute is very handy with text strings, and it can output a layer, a comp,
or a footage element's name, or even a property name as a text string.
Now, you can manipulate this with a substring method.
So, click into the expression field, and right after name add a period operator.
And we're going to start the substring method, so substr, followed by an open parenthesis,
and we're going to set the first argument.
So this is going to be the start point, and I'm going to set it to 0,
which begins the substring right before the first character.
After this, add a comma, and then you're going to add the second argument
which is going to be the substring length.
So, for this example, you can use 5, and this is going to output the first 5 characters.
And then, end this with a close parenthesis.
So if you click away from the expression, you'll notice it's just given us 306A:
and that's 5 characters long.
If I were to come down and change this to 7, for example,
now it's going to add space and S. So I'm going to make this 5 again.
Now, I'm going to take this a step further and go to the end of the expression,
and add a Addition operator, and then a new text string inside of double quotations.
And it's going to say space, learning.
And after this I'm going to add another addition operator.
And I'm going to create another substring, just like the first one,
so name, to reference the layer name, period operator, substr, open parenthesis,
this one's going to start at 5, comma, and now I want this to go to the very end.
But how do I do that?
What if it's 2 characters long, what if it's 20?
A good way to do this is to use name.length
And the length attribute is going to always output
the maximum number of characters that text string has.
So if I end this with a close parenthesis and click away from the expression,
we have a totally different output.
So some of this is the layer name, 306A here, and substrings here, and we've inserted
space, learning right here in the middle.
So it's kind of a hybrid between the layer name and a custom text string.
So I can actually come over to the layer, and hit Enter to edit the name,
and I'm just going to replace this 306A with Enjoy.
Now if I hit Enter to save the name, you'll see that the text has actually changed here
in the composition to Enjoy Learning Substrings.
Here's some more examples of substrings.
This expression outputs the first two characters of a text string or input.
This expression does the exact opposite and outputs every character from the third character
to the end of the text string or input.
This expression outputs the last character of the text string or input, regardless of length.
When working with strings, you may need to convert a string to a number
with the parse integer method for working with math functions.
This is especially useful when extracting a composition or layer name,
which includes a number, into an actionable value or integer.
The parse integer method has three parts, the method name, parseInt, all lowercase
with a capital I, followed by a single argument wrapped in parenthesis,
which tells the method what to convert to an integer.
Let's look at the 306B text layer.
Say you want to convert the number in the comp name, 306, to a numerical value
for use with a property like Scale.
First, you can open the Scale property by pressing S on your keyboard,
and add a new expression to the Scale property.
Now we're going to override the default expression here and you can start with a variable name.
So I'm going to use src, equals, and now you're going to start the parse integer method,
so parseInt with a capital I, followed by an open parenthesis, and a reference to the current comp name.
So thisComp, capital C, dot name.
Now you can close parenthesis and end the line with a semi colon.
On the next line, create a two dimensional array with square brackets and reference the src variable.
So open square bracket, src, comma, space, src, close square bracket.
And this is it for the expression, so if you click away from the expression field,
you'll see that the text layer has gotten pretty big here.
It's Scale is set to 306 which is also the number in the comp name.
And this expression is parsing the comp name and outputting the number as a result.
So if you come over to the Project Panel and hit Enter to change the name,
and change this to, I don't know, something like 150, and then hit Enter again to save.
You'll notice that the text Scale now reads 150 which is the name of the comp.
Here's some examples of how the parse integer method interprets various text strings.
This text string is returned as 1, without the 0.
This text string is returned as 10.
This text string is returned as 2 and all the rest of the characters are omitted.
This text string returns an error, not a number, because the first character is not a number.
This text string returns 1 and not 13, because it finds the first number
and then omits everything else after it.
Regular Expressions are complex methods of searching text, not to be confused
with the After Effects term expressions.
They can search for very specific patterns based on elements you provide it.
A useful application of RegEx is the replace method.
You can use it to find, replace, and output text.
It has three parts, the method name, replace, all lower case, followed by two arguments
enclosed in parenthesis and separated by a comma and a space.
The first argument is the RegEx search parameters.
The second argument specifies what to replace it with.
So in this composition, find the two 306C layers.
Open the Source Text property on each, so I'm going to hit U + U to do that.
Now, the first layer Input has the text here in the composition.
But the second layer which is Output is blank, it has no content.
So, first we want to add an expression to the Output Source Text,
so Alt + Click (or Option + Click on a Mac) the stopwatch.
And now, grab the Pick Whip tool and come up to the Input Source Text and let go.
Now, it's going to give you automatic syntax referencing that property.
So come to the very end and add a period operator, followed by the replace method.
So, replace, followed by an open parenthesis, and now you can begin the RegEx search.
A simple Regular Expression can take the form of the word or phrase you want to find.
So to do this, type out the word exactly the way you expect it to appear,
and then surround it with forward slashes.
In this case, you can type forward slash source, and then another forward slash,
to search for any instance of source within your input text string.
So after the last forward slash, you'll want to add two letters, g and i.
These two letters are called flags and they instruct the RegEx engine to search globally
for this string, and for the search to be non case sensitive.
After the gi flags, add a comma and a space, followed by the text string that
you want to replace it with wrapped in double quotations.
So in this case, I want to replace source with final output.
And make sure it's surrounded in double quotes.
And finally finish it with a close parenthesis.
So this is the entire replace method using our RegEx search parameters, and giving it
the text string to replace it with.
So if you click away from the expression, you'll see that not only does it now have text,
but it's reading the RegEx Source layer and taking the content,
and replacing source with final output.
Now you can also daisy chain these together.
So if you go to the very end and create another replace, open parenthesis, forward slash,
and I'm going to replace RegEx, search globally, and non case sensitive, comma space,
and then I'm going to replace this with this is the, quotation, close parenthesis.
So now, you see that there are two replace methods daisy chained right after another.
And if you click away from the expression, it reads this is the final output.
We've barely scratched the surface of this deep and powerful tool.
If you're interested in learning more, look for Regular Expression resources online.
We've arrived at the end of Lesson 306: Working With Advanced Text Methods
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.