Chapter 17: Working with strings
declare namespace functx = "http://www.functx.com";
declare function functx:contains-word
($string as xs:string?, $word as xs:string) as xs:boolean
{
let $upString := upper-case($string)
let $upWord := upper-case($word)
return matches($upString, concat("^(.*\W)?", $upWord, "(\W.*)?$"))
};
Table 17-2. Examples of contains
, starts-with
, and ends-with
Example | Return value |
contains("query", "ery") | true |
contains("query", "query") | true |
contains("query", "x") | false |
starts-with("query", "que") | true |
starts-with("query", "u") | false |
ends-with("query", "y") | true |
ends-with("query ", "y") | false |
Table 17-3. Examples of the matches
function
Example | Return value |
matches("query", "q") | true |
matches("query", "qu") | true |
matches("query", "xyz") | false |
matches("query", "q.*") | true |
matches("query", "[a-z]{5}") | true |
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
};
Table 17-4. Examples of the substring functions
Example | Return value |
substring("query", 2, 3) | uer |
substring("query", 2) | uery |
substring-before("query", "er") | qu |
substring-before("queryquery", "er") | qu |
substring-after("query", "er") | y |
substring-after("queryquery", "er") | yquery |
declare namespace functx = "http://www.functx.com";
declare namespace functx = "http://www.functx.com";
declare function functx:set-string-to-length
($stringToPad as xs:string?, $padChar as xs:string,
$length as xs:integer) as xs:string
{
substring(
string-join (($stringToPad, for $i in (1 to $length) return $padChar),"")
,1,$length)
};
Table 17-5. Examples of the string-length
function
Example | Return value |
string-length("query") | 5 |
string-length(" query ") | 9 |
string-length(normalize-space(" query ")) | 5 |
string-length("") | 0 |
string-length(" ") | 1 |
Table 17-7. Examples of the tokenize
function
Example | Return value |
tokenize("a b c", "\s") | ("a", "b", "c") |
tokenize("a b c", "\s+") | ("a", "b", "c") |
tokenize("a-b—c", "-") | ("a", "b", "", "c") |
tokenize("-a-b-", "-") | ("", "a", "b", "") |
tokenize("a/ b/ c", "[/\s]+") | ("a", "b", "c") |
tokenize("2006-12-25T12:15:00", "[\-T:]") | ("2006","12","25","12","15","00") |
tokenize("Hello, there.", "\W+") | ("Hello", "there") |
Table 17-9. Examples of the upper-case
and lower-case
functions
Example | Return value |
upper-case("query") | QUERY |
upper-case("Query") | QUERY |
lower-case("QUERY-123") | query-123 |
lower-case("Query") | query |
Table 17-10. Examples of the replace
function
Example | Return value |
replace("query", "r", "as") | queasy |
replace("query", "qu", "quack") | quackery |
replace("query", "[ry]", "l") | quell |
replace("query", "[ry]+", "l") | quel |
replace("query", "z", "a") | query |
replace("query", "query", "") | A zero-length string |
declare namespace functx = "http://www.functx.com";
declare function functx:replace-first
($string as xs:string?, $pattern as xs:string,
$replacement as xs:string) as xs:string
{
replace($string, concat("(^.*?)", $pattern), concat("$1",$replacement))
};
Table 17-11. Examples of the normalize-space
function
Example | Return value |
normalize-space("query") | query |
normalize-space(" query ") | query |
normalize-space("xml query") | xml query |
normalize-space("xml query") | xml query |
normalize-space(" ") | A zero-length string |