Eyeon:Script/Reference/Libraries/string/find

From VFXPedia

< Eyeon:Script | Reference | Libraries | string
Revision as of 14:01, 9 December 2006 by Peter (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

String : string.find

Arguments

string.find( s, pattern, index )

  • s (required, string)

The string to be searched

  • pattern (required, string)

The pattern to be searched for

  • index (optional, number)

Where to start searching for pattern in string 's'


Returns

Two numbers that represent the start and end location of the pattern in the string. If no match is found for the pattern then this function will return nil.

Remarks

The basic use of string.find() is to search for an instance of a pattern inside a provided string (called the subject string). The function returns the index where the pattern starts and stops, or nil if the pattern was not found.

Requirements

  • eyeonScript 5.0
  • Fusion 5.0

Examples

-- returns 1 and 5 s = "hello world" i, j = string.find(s, "hello") print(i, j)


Tips for find (edit)

string.find uses regular expressions which means that some characters (like dots or asterisks) have special meanings and mess with your expected result. For example, if you want to test for a certain file extension by using

string.find(mypath, ".comp")

you will be surprised get a positive result on a file called "too_complicated.jpg". This is because in regular expressions, the dot is a wildcard for any character. To actually search for the desired file extensions, use this:

-- % turns the dot into an actual dot instead of a wildcard
-- $ only allows the hit to occur at the end of the string
string.find(mypath, "%.comp$")

There's a fourth parameter to this function. According to the LUA referece manual:

A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well.

Thus, a more correct version without regular expressions would be this:

string.find(mypath, ".comp", 1, true)

However, regular expressions are still superior because the command above doesn't require ".comp" to be at the end of the file name.