Three functions are available to return part of a string. The substring
function returns a substring based on a starting position (starting at 1 not 0) and optionally a length. For example:
substring("query", 2, 3)
returns the string uer
. If no length is specified, the function returns the rest of the string. For example:
substring("query", 2)
returns uery
.
The substring-before
function returns all the characters of a string that occur before the first occurrence of another specified string. The substring-after
function returns all the characters of a string that occur after the first occurrence of another specified string. Table 17-4 shows examples of the substring functions.
USEFUL FUNCTION: substring-after-last
If you want the substring that appears after the last occurrence of the specified string, you can use the substring-after-last
function, shown here:
declare namespace functx = "http://www.functx.com"; declare function functx:substring-after-last ($string as xs:string?, $delim as xs:string) as xs:string? { if (contains ($string, $delim)) then functx:substring-after-last(substring-after($string, $delim), $delim) else $string };
For example, calling this function with:
functx:substring-after-last("2006-05-03", "-")
will return 03
. This function uses recursion to call the substring-after
function repeatedly until the string no longer contains the search characters.
13.58.116.51