String slicing with APOC

Hi all,
I'm struggling with APOC text functions to filter nodes by matching a string with a slice of a string in nodes' attributes.

In python, I can easily do that by treating strings as arrays:

test = "a test string"
'test' == test[2:6]

How can I accomplish the same results with Cypher/APOC ? That is, do a partial match in parts of a string?

Hi, depending on whether you need the exact place of the string or not you have several options:

  • RETURN test CONTAINS "test" // returns true
  • RETURN apoc.text.indexOf(test, 'test') // returns 2
  • Or you can use STARTS WITH or ENDS WITH

Hope this helps :wink:

Also CONTAINS works too. The advantage of STARTS WITH, ENDS WITH, and CONTAINS is that these are index-friendly, an index on the property allows leveraging of the index.

While we can't currently do index-based slicing of a string in the way that we can for lists (I'm pushing for this, myself), we do have the built-in substring() function.