12.10. String functions

12.10.1. STR
12.10.2. REPLACE
12.10.3. SUBSTRING
12.10.4. LEFT
12.10.5. RIGHT
12.10.6. LTRIM
12.10.7. RTRIM
12.10.8. TRIM
12.10.9. LOWER
12.10.10. UPPER
12.10.11. Case Expressions
12.10.12. Simple CASE
12.10.13. Generic CASE

These functions all operate on string expressions only, and will return an error if used on any other values. Except STR(), which converts to strings.

See also Section 11.1.4, “String operators”.

Figure 12.9. Graph


12.10.1. STR

STR returns a string representation of the expression.

Syntax: STR( expression )

Arguments:

  • expression: An expression that returns anything

Query. 

MATCH a
RETURN str(1)
LIMIT 1

A string.

Result

str(1)
1 row

"1"


12.10.2. REPLACE

REPLACE returns a string with the search string replaced by the replace string. It replaces all occurrences.

Syntax: REPLACE( original, search, replace )

Arguments:

  • original: An expression that returns a string
  • search: An expression that returns a string to search for
  • replace: An expression that returns the string to replace the search string with

Query. 

MATCH a
RETURN replace("hello", "l", "w")
LIMIT 1

A string.

Result

replace("hello", "l", "w")
1 row

"hewwo"


12.10.3. SUBSTRING

SUBSTRING returns a substring of the original, with a 0-based index start and length. If length is omitted, it returns a substring from start until the end of the string.

Syntax: SUBSTRING( original, start [, length] )

Arguments:

  • original: An expression that returns a string
  • start: An expression that returns a positive number
  • length: An expression that returns a positive number

Query. 

MATCH n
RETURN substring("hello", 1, 3), substring("hello", 2)
LIMIT 1

A string.

Result

substring("hello", 1, 3)substring("hello", 2)
1 row

"ell"

"llo"


12.10.4. LEFT

LEFT returns a string containing the left n characters of the original string.

Syntax: LEFT( original, length )

Arguments:

  • original: An expression that returns a string
  • n: An expression that returns a positive number

Query. 

MATCH n
RETURN left("hello", 3)
LIMIT 1

A String.

Result

left("hello", 3)
1 row

"hel"


12.10.5. RIGHT

RIGHT returns a string containing the right n characters of the original string.

Syntax: RIGHT( original, length )

Arguments:

  • original: An expression that returns a string
  • n: An expression that returns a positive number

Query. 

MATCH n
RETURN right("hello", 3)
LIMIT 1

A string.

Result

right("hello", 3)
1 row

"llo"


12.10.6. LTRIM

LTRIM returns the original string with whitespace removed from the left side.

Syntax: LTRIM( original )

Arguments:

  • original: An expression that returns a string

Query. 

MATCH n
RETURN ltrim("   hello")
LIMIT 1

A string.

Result

ltrim(" hello")
1 row

"hello"


12.10.7. RTRIM

RTRIM returns the original string with whitespace removed from the right side.

Syntax: RTRIM( original )

Arguments:

  • original: An expression that returns a string

Query. 

MATCH n
RETURN rtrim("hello   ")
LIMIT 1

A string.

Result

rtrim("hello ")
1 row

"hello"


12.10.8. TRIM

TRIM returns the original string with whitespace removed from both sides.

Syntax: TRIM( original )

Arguments:

  • original: An expression that returns a string

Query. 

MATCH n
RETURN trim("   hello   ")
LIMIT 1

A string.

Result

trim(" hello ")
1 row

"hello"


12.10.9. LOWER

LOWER returns the original string in lowercase.

Syntax: LOWER( original )

Arguments:

  • original: An expression that returns a string

Query. 

MATCH n
RETURN lower("HELLO")
LIMIT 1

A string.

Result

lower("HELLO")
1 row

"hello"


12.10.10. UPPER

UPPER returns the original string in uppercase.

Syntax: UPPER( original )

Arguments:

  • original: An expression that returns a string

Query. 

MATCH a
RETURN upper("hello")
LIMIT 1

A string.

Result

upper("hello")
1 row

"HELLO"


12.10.11. Case Expressions

Cypher supports CASE expressions, which is a generic conditional expression, similar to if/else statements in other languages. Two variants of CASE exist - the simple form and the generic form.

12.10.12. Simple CASE

The expression is calculated, and compared in order with the WHEN clauses until a match is found. If no match is found the expression in the ELSE clause is used, or null, if no ELSE case exists.

Syntax: +CASE+ in +WHEN+ value +THEN+ result [+WHEN+ ...] [+ELSE+ default] +END+

Arguments:

  • in: A valid expression
  • value: An expression whose result will be compared to expression
  • result: This is the result expression used if the value expression matches the in expression
  • default: The expression to use if no match is found.

Query. 

MATCH n
RETURN CASE n.eyes
WHEN 'blue'  THEN 1
WHEN 'brown' THEN 2
ELSE 3
END as result

A string.

Result

result
5 rows

2

1

2

1

3


12.10.13. Generic CASE

The predicates are evaluated in order until a true value is found, and the result value is used.If no match is found the expression in the +ELSE + clause is used, or null, if no + ELSE + case exists.

Syntax: +CASE+ +WHEN+ predicate +THEN+ result [+WHEN+ ...] [+ELSE+ result] +END+

Arguments:

  • predicate: A predicate that is tested to find a valid alternative
  • result: This is the result expression used if the predicate matches
  • default: The expression to use if no match is found.

Query. 

MATCH n
RETURN CASE
WHEN n.eyes = 'blue'  THEN 1
WHEN n.age < 40       THEN 2
ELSE 3
END as result

A string.

Result

result
5 rows

3

1

2

1

3