substring()
returns a substring of the original string, beginning with a 0-based index start and length.
Syntax: substring(original, start [, length])
Returns: A String.
Arguments
Name | Description |
original | An expression that returns a string. |
start | An expression that returns a positive integer, denoting the position at which the substring will begin. |
length | An expression that returns a positive integer, denoting how many characters of original will be returned. |
Example
RETURN substring("Hello, World!", 2, 5)
The value returned will be string which will be sub-string of the original string. The substring will start from index 2 of the original string(inclusive of the character at index 2) and will contain the next 5 characters of the original string. In this case the substring will be “llo W”, i.e , starting from 2nd character of the original string and containing the next 5 characters of the original string.
The 3rd argument(length) is optional, which if not provided, will return a substring from the start index to the end of the original string.
Considerations
uses a zero-based index, i.e, the first character in the original string has 0 index.start
length
is an optional parameter. Iflength
is omitted, the function returns the substring starting at the position given bystart
and extending to the end oforiginal
.- If
original
isnull
,null
is returned. - If
start
isnull
or a negative integer, an error is raised. - If
length
isnull
or a negative integer, an error is raised. - If
start
is0
, the substring will start at the beginning oforiginal
. - If
length
is0
, the empty string will be returned.
Example
RETURN substring("Learning Cypher!", 2)
Returns the following Result
substring("Learning Cypher!", 2) |
"arning Cypher!" |
Example
RETURN substring("Learning Cypher!", 2, 7)
Returns the following Result
substring("Learning Cypher!", 2, 7) |
"arning " |
Example
RETURN substring(null, 2)
Returns the following Result
substring(null, 2) |
null |
Note– substring(null, start)
returns null
.
Example
RETURN substring("Learning Cypher!", 2, 0)
Returns the following Result
substring("Learning Cypher!", 2, 0) |
"" |
Example
RETURN substring("Learning Cypher!", 0, 8)
Returns the following Result
substring("Learning Cypher!", 0, 8) |
"Learning" |
Example
RETURN substring("Learning Cypher!", null, 7)
Returns the following Result
substring("Learning Cypher!", null, 7) |
Error Neo.ClientError.Statement.TypeError |
Note– Negative or Null values of start
raises an error.
Example
RETURN substring("Learning Cypher!", -5, 7)
Returns the following Result
substring("Learning Cypher!", -5, 7) |
Error Neo.DatabaseError.General.UnknownError |
Note– Negative or Null values of start
raises an error.
Example
RETURN substring("Learning Cypher!", 2, null)
Returns the following Result
substring("Learning Cypher!", 2, null) |
Error Neo.ClientError.Statement.TypeError |
Note– Negative or Null values of length
gives an error.
Example
RETURN substring("Learning Cypher!", 2, -7)
Returns the following Result
substring("Learning Cypher!", 2, -7) |
Error Neo.DatabaseError.General.UnknownError |
Note– Negative or Null values of length
gives an error.