Functions on String Values
This topic describes the built-in SPARQL functions that operate on string values.
- CONCAT: Concatenates a list of string values.
- CONTAINS: Returns a boolean value that indicates whether the first string contains the second string.
- ENCODE_FOR_URI: Encodes string values to make them valid URIs. ENCODE_FOR_URI escapes any characters that are not supported in a URI.
- LANGMATCHES: Tests whether the language tags match for string values.
- LCASE: Returns a lowercase version of the input value.
- LEVENSHTEIN_DIST: Calculates the Levenshtein distance or measure of similarity between the specified strings. The distance is the number of edits required to transform the first string into the second string.
- REGEX: Returns a boolean value that indicates whether a string matches the specified pattern.
- REPLACE: Replaces each non-overlapping occurrence of a regular expression with the specified string.
- STRAFTER: Returns the part of a string that appears after the specified substring.
- STRBEFORE: Returns the part of a string that appears before the specified substring.
- STRENDS: Tests whether a string value ends with the specified substring.
- STRLEN: Returns the number of characters in the specified string.
- STRSTARTS: Tests whether a string value starts with the specified substring.
- SUBSTR: Returns a substring of the specified string.
- UCASE: Returns an uppercase version of the input value. UCASE supports the lower ASCII character set and leaves numeric values unchanged.
CONCAT Syntax
Use the following syntax when incorporating CONCAT functions in queries:
CONCAT(expression1, expression2 [, expression3 ] [, ...])
Where each expression is a value that you want to concatenate in the return value.
CONTAINS Syntax
Use the following syntax when incorporating CONTAINS functions in queries:
CONTAINS(expression1, expression2)
Where expression1 is tested to see if it contains expression2.
ENCODE_FOR_URI, LCASE, UCASE Syntax
Use the following syntax when incorporating ENCODE_FOR_URI, LCASE, or UCASE functions in queries:
FUNCTION(string_literal)
Where string_literal is the value to encode as a URI or convert to lower or upper case
LANGMATCHES Syntax
Use the following syntax when incorporating LANGMATCHES functions in queries:
LANGMATCHES(expression, language_identifier)
Where expression is tested to see if it includes the language tag specified in language_idenitifer.
LEVENSHTEIN_DIST Syntax
Use the following syntax when incorporating LEVENSHTEIN_DIST functions in queries:
LEVENSHTEIN_DIST(string1, string2)
REGEX Syntax
Use the following syntax when incorporating REGEX functions in queries:
REGEX(expression, pattern [, flag ])
Where expression is tested to see if it includes the specified regular expression pattern. The optional flag argument includes one or more modifiers that further define the pattern.
For information about the supported regular expression syntax, see the Regular Expression Syntax section of the W3C XQuery 1.0 and XPath 2.0 Functions and Operators specification. For information about the supported modifier flags, see the Flags section of the same document.
REPLACE Syntax
Use the following syntax when incorporating REPLACE functions in queries:
REPLACE(expression, pattern, replacement_string [, flag ])
Where expression is tested to see if it includes the specified regular expression pattern. If expression does include pattern, the pattern is replaced by the replacement_string. The optional flag argument includes one or more modifiers that further define the pattern.
For information about the supported regular expression syntax, see the Regular Expression Syntax section of the W3C XQuery 1.0 and XPath 2.0 Functions and Operators specification. For information about the supported modifier flags, see the Flags section of the same document.
STRAFTER Syntax
Use the following syntax when incorporating STRAFTER functions in queries:
STRAFTER(expression, substring)
Where expression is the string literal value to find the substring in. The resulting value is the part of the expression that comes after the substring.
STRBEFORE Syntax
Use the following syntax when incorporating STRBEFORE functions in queries:
STRBEFORE(expression, substring)
Where expression is the string literal value to find the substring in. The resulting value is the part of the expression that comes before the substring.
STRENDS Syntax
Use the following syntax when incorporating STRENDS functions in queries:
STRENDS(expression, end_string)
Where expression is the string value to find the end_string in. STRENDS returns true if expression ends in end_string and false if it does not.
STRLEN Syntax
Use the following syntax when incorporating STRLEN functions in queries:
STRLEN(string_literal)
STRLEN returns an integer that indicates the number of characters in string_literal.
STRSTARTS Syntax
Use the following syntax when incorporating STRSTARTS functions in queries:
STRSTARTS(expression, start_string)
Where expression is the string value to find the start_string in. STRSTARTS returns true if expression starts in start_string and false if it does not.
SUBSTR Syntax
Use the following syntax when incorporating SUBSTR functions in queries:
SUBSTR(string_literal, start_location, [ length ])
Where start_location is an integer that defines the starting character of the substring by counting that number of characters from the beginning of the string_literal. The optional length parameter is an integer that defines the total number of characters to return in the resulting substring.
String Function Examples
The following example query uses STRAFTER to return only the unique portion of each event ID in the sample Tickit data set. The query uses BIND to convert the event URIs to strings and bind them to the ?str_event variable.
SELECT (STRAFTER(?str_event, "event") AS ?event_number) ?name FROM <tickit> WHERE { ?event <eventname> ?name . BIND (STR(?event) AS ?str_event) } ORDER BY ?event_number
event_number | name -------------+--------------------------------- 1 | Gotterdammerung 10 | Rigoletto 100 | Siegfried 1000 | Gypsy 1001 | Chicago 1002 | The King and I 1003 | Pal Joey 1004 | Grease ... 8798 rows