cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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]
True

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

1 ACCEPTED SOLUTION

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

View solution in original post

2 REPLIES 2

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

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.