Four functions can be used to manipulate the characters of a string. They are listed in Table 17-8.
Table 17-8. Functions that manipulate strings
Name |
Description |
---|---|
|
Translates a string into uppercase equivalents |
|
Translates a string into lowercase equivalents |
|
Replaces individual characters with other individual characters |
|
Replaces characters that match a regular expression with a specified string |
The upper-case
and lower-case
functions are used to convert a string to all uppercase or lowercase. For example, upper-case("Query")
returns QUERY
. The mappings between lowercase and uppercase characters are determined by Unicode case mappings. If a character does not have a corresponding uppercase or lowercase character, it is included in the result string unchanged. Table 17-9 shows some examples.
The translate
function is used to replace individual characters in a string with other individual characters. It takes three arguments:
The string to be translated
The list of characters to be replaced (as a string)
The list of replacement characters (as a string)
Each character in the second argument is replaced by the character in the same position in the third argument. For example:
translate("**test**321", "*123", "-abc")
returns the string --test--cba.
If the second argument is longer than the third argument, the extra characters in the second argument are simply omitted from the result. For example:
translate("**test**321", "*123", "-")
returns the string --test--
.
The replace
function is used to replace nonoverlapping substrings that match a regular expression with a specified replacement string. It takes three arguments:
The string to be manipulated
The pattern, which uses the regular expression syntax described in Chapter 18
The replacement string
While it is nice to have the power of regular expressions, you don't have to be familiar with regular expressions to replace a particular sequence of characters; you can simply specify the string you want replaced for the $pattern
argument, as long as it doesn't contain any special characters.
An optional fourth argument allows for additional options in the interpretation of the regular expression, such as multi-line processing and case sensitivity. Table 17-10 shows some examples.
XQuery also supports variables in the replacement text, which allow parenthesized subexpressions to be referenced by number. You can use the variables $1
through $9
to represent the first nine parenthesized expressions in the pattern. This is very useful when replacing strings, on the condition that they come directly before or after another string. For example, if you want to change instances of the word Chap to the word Sec, but only those that are followed by a space and a digit, you can use the function call:
replace("Chap 2...Chap 3...Chap 4...", "Chap (d)", "Sec $1.0")
which returns Sec 2.0...Sec 3.0...Sec 4.0...
. Subexpressions are discussed in more detail in "Using Sub-Expressions with Replacement Variables" in Chapter 18.
3.133.158.36