Skip to main content

contains

Tests if the input data contains a specific item. Returns a Boolean true or false.

The function is highly useful in filtering operations — see the examples for guidance.

Parameters

  • INPUT (string, list, column, row, JSON array)

    The input data.

  • SEARCH ITEM (string, number, list, JSON object)

    The item to search for.

Notes

  • The function is case-sensitive.

  • If the input data is a string, the search item must also be a string.

  • If the input data is a list, column, row, or array, the search item can be any data type.

  • The function returns true only if it finds an exact match for the relevant data type. The expression [[myList = (1, 3, 7, 2, 4); contains(myList, '7')]] returns false because the input data contains numbers whereas the search item is a string.

  • If the search item is a list, the function returns true only if it finds an exact sequential match in the data. For example, if the input list is (1, 2, 3, 4) and the search list is (1, 3, 2), the function returns false.

Examples

These examples use a hard-coded string or list for the input data:

ATL in Script

Result

[[myString = '23 June 1912'; contains(myString, '23 June')]]

true

[[myString = '23 June 1912'; contains(myString, 'June 23')]]

false

[[myString = '23 June 1912'; contains(myString, '23 june')]]

false

[[myList = makeList(1,3,7,2,4); contains(myList, 7)]]

true

[[myList = makeList(1,3,7,2,4); contains(myList, '7')]]

false

[[myList = makeList(1,3,7,2,4); contains(myList, (7,2,4))]]

true

[[myList = makeList(1,3,7,2,4); contains(myList, (7,4,2))]]

false

Typically, the input data is a list-like object such as a table column.

Table examples

Assume a "Describe the Table" project with this data:

ID

Region

Branch

Sales

Row 1

1001

Southeast

Atlanta

357589.32

Row 2

1002

Northeast

Boston

294293.49

Row 3

1003

Midwest

Chicago

403603.17

Row 4

1004

South

Dallas

324722.58

Row 5

1005

South

Houston

306555.26

Row 6

1006

West

Los Angeles

457359.45

Row 7

1007

Midwest

Milwaukee

192238.52

Row 8

1008

Northeast

New York

468745.37

The first parameter can take a column variable.

ATL in Script

Result

[[contains(Region, 'South')]]

true

[[contains(Region, 'south')]]

false

[[contains(Region, ('Southeast', 'Northeast'))]]

true

[[contains(Region, ('Northeast', 'Southeast'))]]

false

The last result is false because although the Region column contains 'Northeast' and 'Southeast' values, they don't appear in the given order. Contrast this with the third example.

Note

In a "Describe Row in Context" project, you would use RegionColumn for the column variable.

Using contains to filter table data

You can combine contains and filterRows to filter your table.

To get rows with a Region value containing the string "South":

[[filterRows(WholeTable, 'Region', x -> contains(x, 'South'))]]

The filtered table region:

 

Region

Branch

Sales

1001

Southeast

Atlanta

357589.32

1004

South

Dallas

324722.58

1005

South

Houston

306555.26

Note

To get exact matches for "South", use [[filterRows(WholeTable, 'Region', eq('South'))]].

To get rows with a Region value that does NOT contain "South":

[[filterRows(WholeTable, 'Region', x -> !contains(x,'South'))]]

The filtered table region:

 

Region

Branch

Sales

1002

Northeast

Boston

294293.49

1003

Midwest

Chicago

403603.17

1006

West

Los Angeles

457359.45

1007

Midwest

Milwaukee

192238.52

1008

Northeast

New York

468745.37

Note

To filter out "South" only — that is, keep rows with "Southeast" — the ATL is:

[[filterRows(WholeTable, 'Region', x -> x != 'South')]]

To get rows with a Region value of "Midwest" OR "Northeast":

[[

wanted = makeList('Midwest', 'Northeast')

filterRows(WholeTable,'Region', x -> contains(wanted, x))

]]

The first line defines a list of wanted values. The second line filters the table by searching the wanted list for each row's Region value. When it finds a match, the row passes the filter.

The filtered table:

 

Region

Branch

Sales

1002

Northeast

Boston

294293.49

1003

Midwest

Chicago

403603.17

1007

Midwest

Milwaukee

192238.52

1008

Northeast

New York

468745.37

Using contains to filter JSON data

Assume a "Describe the JSON Object" project with this data:

{
    "finances": [
        {
            "branch": "Boston",
            "region": "East",
            "sales": 294293.49
        },
        {
            "branch": "Chicago",
            "region": "Central",
            "sales": 403603.17
        },
        {
            "branch": "Los Angeles",
            "region": "West",
            "sales": 457359.45
        },
        {
            "branch": "New York",
            "region": "East",
            "sales": 458745.37
        },
        {
            "branch": "Philadelphia",
            "region": "East",
            "sales": 357856.14
        }
    ]
}

You can combine filter and contains to get a filtered array.

For example, to get the JSON objects for the East region AND filter out New York:

[[filter(WholeJSON.finances, x -> x.region == 'East' && !contains(x.branch, 'New York'))]]

The filtered array:

{
    "finances": [
        {
            "branch": "Boston",
            "region": "East",
            "sales": 294293.49
        },
        {
            "branch": "Philadelphia",
            "region": "East",
            "sales": 357856.14
        }
    ]
}

You could then use the filtered array to produce narrative text. For example:

[[

filteredArray = filter(WholeJSON.finances, x -> x.region == 'East' && !contains(x.branch, 'New York'))

filteredSales = totalVal(map(filteredArray, x -> x.sales))

joinStrings('Without New York, the East region achieved sales of ', currencyFormat(filteredSales), '.')

]] 

The output text is:

Without New York, the East region achieved sales of $652,149.63.