Skip to content

Commit 36c4080

Browse files
more examples
1 parent adb7a1a commit 36c4080

File tree

1 file changed

+107
-9
lines changed

1 file changed

+107
-9
lines changed

modules/ROOT/pages/expressions/string-operators.adoc

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= String concatenation operators
2-
:description: Information about Cypher's string operators.
2+
:description: Information about Cypher's string concatenation operators.
33
:table-caption!:
44

55
Cypher contains two operators for the concatenation of `STRING` values:
@@ -10,15 +10,15 @@ Cypher contains two operators for the concatenation of `STRING` values:
1010
The two operators are functionally equivalent.
1111
However, `||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], while `+` is not.
1212

13-
For additional expressions that evaluate `STRING` values, see xref:functions/string.adoc[String functions].
13+
For additional expressions that evaluate to `STRING` values, see xref:functions/string.adoc[String functions].
1414

1515
[[examples]]
1616
== Examples
1717

1818
.`||` and `+`
1919
[source, cypher]
2020
----
21-
RETURN 'Neo' || '4j' AS result1
21+
RETURN 'Neo' || '4j' AS result1,
2222
'Neo' + '4j' AS result2
2323
----
2424

@@ -87,12 +87,48 @@ RETURN p.fullName AS fullName
8787
1+d| Rows: 1
8888
|===
8989

90+
.Adding separators in `STRING` concatenation
91+
[source, cypher]
92+
----
93+
RETURN 'Hello' || ', ' || 'World' AS result
94+
----
95+
96+
.Result
97+
[role="queryresult",options="header,footer",cols="1*<m"]
98+
|===
99+
| result
100+
101+
| "Hello, World"
102+
103+
1+d| Rows: 1
104+
|===
105+
106+
.Adding prefix, suffix, and separators in `STRING` concatenation
107+
[source, cypher]
108+
----
109+
RETURN 'My favorite fruits are: ' || 'apples' || ', ' || 'bananas' || ', and' || 'oranges' || '.' AS result
110+
----
111+
112+
.Result
113+
[role="queryresult",options="header,footer",cols="1*<m"]
114+
|===
115+
| result
116+
117+
| "My favorite fruits are: apples, bananas, and oranges."
118+
119+
1+d| Rows: 1
120+
|===
121+
122+
[[list-values-and-null]]
123+
== String concatenation,`LIST` values, and `NULL`
124+
90125
`STRING` values in a `LIST` can be concatenated using the xref:functions/list.adoc#functions-reduce[`reduce()`] function.
91126

92127
.Concatenate `STRING` values in a `LIST`
93128
[source, cypher]
94129
----
95-
RETURN reduce(acc = '', item IN ['Neo', '4j'] | acc || item) AS result
130+
WITH ['Neo', '4j'] AS list
131+
RETURN reduce(acc = '', item IN list| acc || item) AS result
96132
----
97133

98134
.Result
@@ -105,25 +141,87 @@ RETURN reduce(acc = '', item IN ['Neo', '4j'] | acc || item) AS result
105141
1+d| Rows: 1
106142
|===
107143

144+
The following query uses the xref:functions/scalar.adoc#functions-head[`head()`] function to start the accumulator with the first `item` in the `list`, while the xref:functions/scalar.adoc#functions-tail[`tail()`] function returns the remaining items in the `list`.
145+
The `reduce()` function then concatenates these items with commas.
146+
147+
.Add prefix and a separator (`,`) in a `STRING` concatenated from `STRING` values in a `LIST`
148+
[source, cypher]
149+
----
150+
WITH ['Apples', 'Bananas', 'Oranges'] AS list
151+
RETURN 'My favorite fruits are: ' || reduce(acc = head(list), item IN tail(list) | acc || ', ' || item) || '.' AS result
152+
----
153+
154+
.Result
155+
[role="queryresult",options="header,footer",cols="1*<m"]
156+
|===
157+
| result
158+
159+
| "My favorite fruits are: Apples, Bananas, Oranges."
160+
161+
1+d| Rows: 1
162+
|===
163+
108164
Concatenating a `STRING` value with `NULL` returns `NULL`.
109165
To skip the first `NULL` value in a list of expressions, use the xref:functions/scalar.adoc#functions-coalesce[`coalesce()`] function.
110166

111-
In the following example, `coalesce()` is used with `reduce()` to replace each `NULL` value in the `LIST` with an empty `STRING` (`''`).
167+
In the following query, `coalesce()` is used with `reduce()` to replace each `NULL` value in the `LIST` with an empty `STRING` (`''`).
112168
This ensures that all `NULL` values are effectively skipped, allowing the `reduce()` function to concatenate the remaining `STRING` values.
113169

114170
.Using `reduce()` and `coalesce()` to skip `NULL` values when concatenating a `LIST`
115171
[source, cypher]
116172
----
117-
WITH ['Neo', NULL, '4j', NULL, 'Hello', NULL] AS items
118-
RETURN reduce(acc = '', item IN items | acc || coalesce(item, '')) AS result
173+
WITH ['Apples', NULL, 'Bananas', NULL, 'Oranges', NULL] AS list
174+
RETURN 'My favorite fruits are: ' || reduce(acc = head(list), item IN tail(list) | acc || coalesce(', ' || item, '')) || '.' AS result
175+
----
176+
177+
.Result
178+
[role="queryresult",options="header,footer",cols="1*<m"]
179+
|===
180+
| result
181+
182+
| "My favorite fruits are: Apples, Bananas, Oranges."
183+
184+
1+d|Rows: 1
185+
|===
186+
187+
If a `LIST` is empty, `reduce()` will return `NULL` because there are no elements to process.
188+
In such cases, `coalesce()` can be used to replace the `NULL` with a default value (e.g., `'none'`).
189+
190+
.Using `reduce()` and `coalesce()` to handle empty `LIST` values
191+
[source, cypher]
192+
----
193+
UNWIND [['Apples', 'Bananas', 'Oranges'], ['Pears'], []] AS list
194+
RETURN 'My favorite fruits are: ' || coalesce(reduce(acc = head(list), item IN tail(list) | acc || ', ' || item), 'none') || '.' AS result
119195
----
120196

121197
.Result
122198
[role="queryresult",options="header,footer",cols="1*<m"]
123199
|===
124200
| result
125201

126-
| "Neo4jHello"
202+
| "My favorite fruits are: Apples, Bananas, Oranges."
203+
| "My favorite fruits are: Pears."
204+
| "My favorite fruits are: none."
205+
206+
1+d|Rows: 3
207+
|===
208+
209+
Finally, xref:values-and-types/lists.adoc#cypher-list-comprehension[
210+
list comprehension] allows concatenating a `STRING` value to each item in a `LIST` to generate a new `LIST` of modified `STRING` values.
211+
212+
.List comprehension with `STRING` concatenation on `LIST` items
213+
[source, cypher]
214+
----
215+
WITH ['Apples', 'Bananas', 'Oranges'] AS list
216+
RETURN [item IN list | 'Eat more ' || item || '!'] AS result
217+
----
218+
219+
.Result
220+
[role="queryresult",options="header,footer",cols="1*<m"]
221+
|===
222+
| result
223+
224+
| ["Eat more Apples!", "Eat more Bananas!", "Eat more Oranges!"]
127225

128226
1+d| Rows: 1
129-
|===
227+
|===

0 commit comments

Comments
 (0)