Split a string by a delimiter
Solution
You can use the Map
, FindStrRegex
, and Concat
functions
to achieve the desired result. The following example creates a
user-defined function
which splits an input string on a specified delimiter and returns the results
as elements of an array.
ObjectV(ref: RefV(id = "SplitString", collection = RefV(id = "functions")),ts: LongV(1653676658410000),name: StringV(SplitString),body: QueryV(System.Collections.Generic.Dictionary`2[System.String,FaunaDB.Query.Expr]))
map[body:{[123 34 97 112 105 95 118 101 114 115 105 111 110 34 58 34 52 34 44 34 108 97 109 98 100 97 34 58 91 34 115 116 114 105 110 103 34 44 34 100 101 108 105 109 105 116 101 114 34 93 44 34 101 120 112 114 34 58 123 34 105 102 34 58 123 34 110 111 116 34 58 123 34 105 115 95 115 116 114 105 110 103 34 58 123 34 118 97 114 34 58 34 115 116 114 105 110 103 34 125 125 125 44 34 116 104 101 110 34 58 123 34 97 98 111 114 116 34 58 34 83 112 108 105 116 83 116 114 105 110 103 32 111 110 108 121 32 97 99 99 101 112 116 115 32 115 116 114 105 110 103 115 34 125 44 34 101 108 115 101 34 58 123 34 109 97 112 34 58 123 34 108 97 109 98 100 97 34 58 34 114 101 115 34 44 34 101 120 112 114 34 58 123 34 115 101 108 101 99 116 34 58 91 34 100 97 116 97 34 93 44 34 102 114 111 109 34 58 123 34 118 97 114 34 58 34 114 101 115 34 125 125 125 44 34 99 111 108 108 101 99 116 105 111 110 34 58 123 34 102 105 110 100 115 116 114 114 101 103 101 120 34 58 123 34 118 97 114 34 58 34 115 116 114 105 110 103 34 125 44 34 112 97 116 116 101 114 110 34 58 123 34 99 111 110 99 97 116 34 58 91 34 91 94 92 92 34 44 123 34 118 97 114 34 58 34 100 101 108 105 109 105 116 101 114 34 125 44 34 93 43 34 93 125 125 125 125 125]} name:SplitString ref:{SplitString 0xc0000a0240 0xc0000a0240 <nil>} ts:1654048374850000]
{ref: ref(id = "SplitString", collection = ref(id = "functions")), ts: 1653678061170000, name: "SplitString", body: QueryV({api_version=4, lambda=[string, delimiter], expr={if={not={is_string={var=string}}}, then={abort=SplitString only accepts strings}, else={map={lambda=res, expr={select=[data], from={var=res}}}, collection={findstrregex={var=string}, pattern={concat=[[^\, {var=delimiter}, ]+]}}}}})}
{
ref: Function("SplitString"),
ts: 1653676301750000,
name: 'SplitString',
body: Query(Lambda(["string", "delimiter"], If(Not(IsString(Var("string"))), Abort("SplitString only accepts strings"), Map(FindStrRegex(Var("string"), Concat(["[^\\", Var("delimiter"), "]+"])), Lambda("res", Select(["data"], Var("res")))))))
}
{'ref': Ref(id=SplitString, collection=Ref(id=functions)), 'ts': 1654031180420000, 'name': 'SplitString', 'body': Query({'api_version': '4', 'lambda': ['string', 'delimiter'], 'expr': {'if': {'not': {'is_string': {'var': 'string'}}}, 'then': {'abort': 'SplitString only accepts strings'}, 'else': {'map': {'lambda': 'res', 'expr': {'select': ['data'], 'from': {'var': 'res'}}}, 'collection': {'findstrregex': {'var': 'string'}, 'pattern': {'concat': ['[^\\', '|', ']+']}}}}})}
{
ref: Function("SplitString"),
ts: 1653676193440000,
name: 'SplitString',
body: Query(Lambda(["string", "delimiter"], If(Not(IsString(Var("string"))), Abort("SplitString only accepts strings"), Map(FindStrRegex(Var("string"), Concat(["[^\\", Var("delimiter"), "]+"])), Lambda("res", Select(["data"], Var("res")))))))
}
Once your SplitString
UDF is created, you can use it to split strings on a
specified delimiter.
Arr(StringV(red), StringV(blue), StringV(pink))
[red blue pink]
["red", "blue", "pink"]
[ 'red', 'blue', 'pink' ]
["red", "blue", "pink"]
[ 'red', 'blue', 'pink' ]
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!