DEV Community

Abdul Samad
Abdul Samad

Posted on

Exploring String Functions in Apache AgeDB

Apache AgeDB is a powerful graph database that provides various functionalities for managing and manipulating data. One such set of functions is the string functions, which allow users to perform operations on strings stored within the database. In this blog post, we will delve into the details of some of the string functions provided by Apache AgeDB.

replace()

The replace() function is used to replace all occurrences of a specified string within another string with a different string. It takes three arguments: original, search, and replace. The original argument represents the original string, while search specifies the string to be replaced, and replace represents the replacement string.
Here's an example query that demonstrates the usage of replace():

SELECT * FROM cypher('graph_name', $$ RETURN replace('hello', 'l', 'w') $$) AS (str_array agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be a new string "Hewwo", where all occurrences of the letter "l" in the original string "hello" have been replaced with "w"

split()

The split() function is employed to split a string into multiple substrings based on a specified delimiter. It returns a list of strings resulting from the splitting operation. The function accepts two arguments: original and split_delimiter. The original argument represents the string to be split, while split_delimiter specifies the delimiter used for splitting.
Consider the following query that demonstrates the usage of split():

SELECT * FROM cypher('graph_name', $$ RETURN split('one,two', ',') $$) AS (split_list agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be a list containing two strings: "one" and "two" The original string "one,two" has been split into separate substrings based on the comma (",") delimiter.

left()

The left() function returns a string containing the specified number of leftmost characters from the original string. It takes two arguments: original and length. The original argument represents the source string, while length indicates the number of characters to be extracted from the left.
Let's consider the following query that demonstrates the usage of left():

SELECT * FROM cypher('graph_name', $$ RETURN left('Hello', 3) $$) AS (new_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be a new string "Hel", which consists of the leftmost three characters from the original string "Hello".

right()

The right() function, similar to left(), returns a string containing the specified number of rightmost characters from the original string. It also accepts two arguments: original and length. The original argument represents the source string, while length indicates the number of characters to be extracted from the right.
Let's examine the following query that demonstrates the usage of right():

SELECT * FROM cypher('graph_name', $$ RETURN right('hello', 3) $$) AS (new_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be a new string "llo" which consists of the rightmost three characters from the original string "hello"

substring()

The substring() function is used to extract a substring from the original string based on a specified start index and an optional length. It takes three arguments: original, start, and length. The original argument represents the source string, start denotes the position at which the substring will begin (using a zero-based index), and length (optional) indicates the number of characters to be returned.
Consider the following query that demonstrates the usage of substring():

SELECT * FROM cypher('graph_name', $$ RETURN substring('hello', 1, 3), substring('hello', 2) $$) AS (sub_str1 agtype, sub_str2 agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be two substrings: "ell" and "llo" In the first substring() call, the substring starts at index 1 and has a length of 3. In the second call, the substring starts at index 2, and since no length is specified, it extends to the end of the original string.

rTrim()

The rTrim() function removes trailing whitespace from the original string and returns the modified string. It takes a single argument: original, which represents the source string.
Consider the following query that demonstrates the usage of rTrim():

SELECT * FROM cypher('graph_name', $$ RETURN rTrim(' hello ') $$) AS (trimmed_str agtype);
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string " hello" with the trailing whitespace removed.

lTrim()

The lTrim() function removes leading whitespace from the original string and returns the modified string. It also takes a single argument: original, representing the source string.
Consider the following query that demonstrates the usage of lTrim():

SELECT * FROM cypher('graph_name', $$ RETURN lTrim(' hello ') $$) AS (trimmed_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string "hello " with the leading whitespace removed.

trim()

The trim() function removes both leading and trailing whitespace from the original string and returns the modified string. It also takes a single argument: original, representing the source string.
Consider the following query that demonstrates the usage of trim():

SELECT * FROM cypher('graph_name', $$ RETURN trim(' hello ') $$) AS (trimmed_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string "hello" with both leading and trailing whitespace removed.

toLower()

The toLower() function converts the original string to lowercase and returns the modified string. It takes a single argument: original, representing the source string.
Consider the following query that demonstrates the usage of toLower():

SELECT * FROM cypher('graph_name', $$ RETURN toLower('HELLO') $$) AS (lower_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string "hello" in lowercase.

toUpper()

The toUpper() function converts the original string to uppercase and returns the modified string. It also takes a single argument: original, representing the source string.
Consider the following query that demonstrates the usage of toUpper():

SELECT * FROM cypher('graph_name', $$ RETURN toUpper('hello') $$) AS (upper_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string "HELLO" in uppercase.

reverse()

The reverse() function reverses the order of characters in the original string and returns the modified string. It takes a single argument: original, representing the source string.
Consider the following query that demonstrates the usage of reverse():

SELECT * FROM cypher('graph_name', $$ RETURN reverse('hello') $$) AS (reversed_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be the string "olleh" with the characters reversed.

toString()

The toString() function is used to convert an integer, float, or boolean value to a string. It takes a single argument: expression, which can be a number, boolean, or string.
Consider the following query that demonstrates the usage of toString():
S

ELECT * FROM cypher('graph_name', $$ RETURN toString(11.5), toString('a string'), toString(true) $$) AS (float_to_str agtype, str_to_str agtype, bool_to_str agtype); 
Enter fullscreen mode Exit fullscreen mode

The result of this query would be three strings: "11.5" (converted from a float), "a string" (unchanged since it is already a string), and "true" (converted from a boolean).

Conclusion:

That concludes our exploration of the string functions in Apache AgeDB. These functions provide powerful capabilities for manipulating and transforming strings within the database. By incorporating these functions into your queries, you can efficiently work with textual data in your graph database. Stay tuned for more informative content on Apache AgeDB!

Top comments (0)