Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Text Contained Within String

  • Text Contained Within String

    Posted by Mike LeBel on August 18, 2020 at 7:13 am

    I’d like to write an IF statement that can look at text in a string (MyString) to determine if a word is contained or found within the string.

    Below is the simplest example I could make up on this question. I know this won’t work but I’m trying to figure out the language.

    Is it possible to search for text within it?
    Thanks.

    MyString = "This is my text"
    if (MyString.contains("my") "Yes" else "Nope"

    Andrei Popa replied 5 years, 9 months ago 3 Members · 5 Replies
  • 5 Replies
  • Scott Mcgee

    August 18, 2020 at 8:08 am

    MyString = “This is my text”
    if (MyString.indexOf(“my”)) “Yes” else “Nope”

    You were missing a closing bracket, but you use indexOf() not contains. Unless someone corrects me. Eitherway the above will work for you.

  • Andrei Popa

    August 18, 2020 at 8:16 am

    Here to correct and annoy people ?

    In case indexOf() does not find a value, it returns -1. -1 is truthy, so this always returns Yes.
    In case he intends to use this in the new expression engine, else does not work on the same line with if.


    MyString = "This is my text"
    if (MyString.indexOf("my")!=-1) "Yes"
    else "Nope"

    Andrei
    My Envato portfolio.

  • Scott Mcgee

    August 18, 2020 at 12:01 pm

    Ah yes, my bad. Can’t believe I missed that bit. Hahaha

  • Mike LeBel

    August 18, 2020 at 2:11 pm

    This was all super helpful. I’m also beginning to understand more how to search for things and key words on creative cow. Thank you!!

  • Andrei Popa

    August 18, 2020 at 2:55 pm

    About searching things better. Here is what I suggest:

    You need to learn few keywords like string, number, array.
    Then search what you want to find out with javascript.
    For example, searching “find a word in string javascript” returns 2 results from w3schools which both could of helped you ;D

    Andrei
    My Envato portfolio.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy