Tinderbox 7 Icon

String.contains("pattern")


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Altered: 

 Function   [other Function type actions]

 Item   [operators of similar scope]

 Query Boolean   [other Query Boolean operators]

 Baseline

 


String.contains("pattern")

This operator tests whether pattern matches the referenced string attribute in whole or part (for regular expression). Matches are case-sensitive. Also, but only in an agent context (i.e. in $AgentQuery), case-sensitivity can be overridden by $AgentCaseSensitive. The match gives a Boolean result.

Thus in default settings, String.contains is always:

pattern is one of:

Important: do not omit the enclosing quotes for literal strings or regex. If omitted, Tinderbox will try to evaluate the string as an expression. Doing this may result in the expected result but this is actually a false positive. So, remember to enclose your regex or literals in quotes.

$MyString.contains("pattern") 

For example:

$MyString.contains(pattern)

is true if $MyString matches pattern. This is the equivalent to the older form of AttributeName(pattern) which is now deprecated. Apart from anything else, this newer syntax should remove the confusion over whether/when to use the $ prefix with attribute names in queries. Other more complex usage:

$MyString.contains($MyMatchText) 

$MyString.contains($MyString(agent)) 

$MyString(parent).contains("Tuesday") 

"Any day like Saturday is good".contains($MyDay) 

"Any day like Saturday is good".contains("Saturday") 

Getting the offset of the (first) pattern match

If the regular expression pattern is found the function returns the match offset+1, where offset is the distance from the start of the string to the start of the matched pattern. If there is more than one match, the offset of the first match is returned. Formerly, .contains() returned true if the pattern was found. The '+1' modifier ensures that a match at position zero return a number higher than zero which would otherwise coerce to false. Since 1+offset is always true, no changes are required in existing documents but the function also now gives usable offset information. Thus, if $MyString is "abcdefgehEi":

$MyNumber = $MyString.contains("e") returns 5.

$MyNumber = $MyString.contains("E") returns 10.

$MyNumber = $MyString.contains("eh") returns 8.

Testing "does not contain"

Use a ! prefix to the query argument:

!$MyString.contains("Tuesday") 

Use of parentheses after the !, around the query, can assist Tinderbox's parsing:

!($MyString.contains("Tuesday")) 

Using back-references

In an agent query or if() conditions the function can return back-references to matches of (sub-)strings.

Dealing with inline quote characters

Because pattern is parsed for regular expressions, it may be possible to use the '\xNN' form described here to work around the lack of escaping from single double quotes within strings.