String Operators
String operators provide functionality for string manipulation and comparison in DataLogic-rs rules.
cat
The cat
operator concatenates multiple strings together.
Syntax
{ "cat": [string1, string2, ...] }
Parameters
Parameter | Description |
---|---|
strings | An array of strings or expressions that evaluate to strings |
Example: Simple String Concatenation
Rule:
xxxxxxxxxx
{"cat": ["Hello, ", "world!"]}
Data:
xxxxxxxxxx
{}
Result:
xxxxxxxxxx
"Hello, world!"
Example: Concatenation with Variables
Rule:
xxxxxxxxxx
{"cat": [
"Hello, ",
{"val": "name"},
"! You are ",
{"val": "age"},
" years old."
]}
Data:
xxxxxxxxxx
{"name": "Alice", "age": 30}
Result:
xxxxxxxxxx
"Hello, Alice! You are 30 years old."
substr
The substr
operator extracts a substring from a string, starting at a specified position
and optionally extracting a specified number of characters.
Syntax
{ "substr": [string, start_index, length] }
Parameters
Parameter | Description |
---|---|
string | The input string to extract from |
start_index | The start position (0-indexed) |
length (optional) | The number of characters to extract (if omitted, extracts until end of string) |
Example: Extract Substring
Rule:
xxxxxxxxxx
{"substr": ["DataLogic-rs", 4, 5]}
Data:
xxxxxxxxxx
{}
Result:
xxxxxxxxxx
"Logic"
Example: Extract to End of String
Rule:
xxxxxxxxxx
{"substr": [
{"val": "email"},
5
]}
Data:
xxxxxxxxxx
{"email": "user@example.com"}
Result:
xxxxxxxxxx
"@example.com"
starts_with
The starts_with
operator checks if a string starts with a specified prefix.
Syntax
{ "starts_with": [string, prefix] }
Parameters
Parameter | Description |
---|---|
string | The string to check |
prefix | The prefix to look for at the start of the string |
Example: Check if string starts with prefix
Rule:
xxxxxxxxxx
{"starts_with": [{"var": "text"}, "Hello"]}
Data:
xxxxxxxxxx
{"text": "Hello World"}
Result:
xxxxxxxxxx
true
Example: Case sensitivity
Rule:
xxxxxxxxxx
{"starts_with": [{"var": "text"}, "hello"]}
Data:
xxxxxxxxxx
{"text": "Hello World"}
Result:
xxxxxxxxxx
false
ends_with
The ends_with
operator checks if a string ends with a specified suffix.
Syntax
{ "ends_with": [string, suffix] }
Parameters
Parameter | Description |
---|---|
string | The string to check |
suffix | The suffix to look for at the end of the string |
Example: Check if string ends with suffix
Rule:
xxxxxxxxxx
{"ends_with": [{"var": "text"}, "World"]}
Data:
xxxxxxxxxx
{"text": "Hello World"}
Result:
xxxxxxxxxx
true
Example: Using with conditional
Rule:
xxxxxxxxxx
{"if": [
{"ends_with": [{"var": "filename"}, ".txt"]},
"Text file",
"Other file"
]}
Data:
xxxxxxxxxx
{"filename": "document.txt"}
Result:
xxxxxxxxxx
"Text file"
upper
The upper
operator converts a string to uppercase.
Syntax
{ "upper": string }
Parameters
Parameter | Description |
---|---|
string | The string to convert to uppercase |
Example: Convert string to uppercase
Rule:
xxxxxxxxxx
{"upper": {"var": "text"}}
Data:
xxxxxxxxxx
{"text": "Hello World"}
Result:
xxxxxxxxxx
"HELLO WORLD"
Example: Combining with other operators
Rule:
xxxxxxxxxx
{"upper": {"substr": [{"var": "text"}, 0, 5]}}
Data:
xxxxxxxxxx
{"text": "hello world"}
Result:
xxxxxxxxxx
"HELLO"
lower
The lower
operator converts a string to lowercase.
Syntax
{ "lower": string }
Parameters
Parameter | Description |
---|---|
string | The string to convert to lowercase |
Example: Convert string to lowercase
Rule:
xxxxxxxxxx
{"lower": {"var": "text"}}
Data:
xxxxxxxxxx
{"text": "Hello World"}
Result:
xxxxxxxxxx
"hello world"
Example: Case-insensitive comparison
Rule:
xxxxxxxxxx
{"==": [
{"lower": {"var": "userInput"}},
"hello world"
]}
Data:
xxxxxxxxxx
{"userInput": "HELLO WORLD"}
Result:
xxxxxxxxxx
true
trim
The trim
operator removes whitespace characters from the beginning and end of a string.
Syntax
{ "trim": string }
Parameters
Parameter | Description |
---|---|
string | The string to trim |
Example: Remove leading and trailing whitespace
Rule:
xxxxxxxxxx
{"trim": {"var": "text"}}
Data:
xxxxxxxxxx
{"text": " Hello World "}
Result:
xxxxxxxxxx
"Hello World"
Example: Combine with other operators
Rule:
xxxxxxxxxx
{"starts_with": [
{"trim": {"var": "text"}},
"Hello"
]}
Data:
xxxxxxxxxx
{"text": " Hello World "}
Result:
xxxxxxxxxx
true