Home >
Online Product Documentation >
Table of Contents >
Manipulating Strings
Manipulating Strings
After you obtain a string, you might want to manipulate it and use the result in the query. This section describes functions that allow you to do this. It discusses the following topics:
Concatenating Strings
To concatenate two or more strings, call the
concat()
function. The format is
The
concat()
function returns the concatenation of its arguments.
Determining the Number of Characters in a String
To obtain the number of characters in a string, call the
string-length()
function. The format is
The
string-length()
function returns the number of characters in the string. If you omit the argument, it defaults to the string value of the context node.
Normalizing Strings
To strip leading and trailing white space from a string, call the
normalize-space()
function. The format is
The
normalize-space()
function removes leading and trailing white space. White space consists of spaces, tabs, new lines, and returns.
If there are consecutive internal spaces, the
normalize-space()
function collapses the internal spaces into one space. The
normalize-space()
function returns the string with the extraneous white space removed. If you omit the argument, it defaults to the string value of the context node.
Replacing Characters in Strings with Characters You Specify
To replace characters in a string with other characters, call the
translate()
function. The format is
The
translate()
function looks for characters in the first string that are also in the second string. For each such character, the
translate()
function replaces the character in the first string with a character from the third string. The replacement character is the character in the third string that is in the same position as the character in the second string that corresponds to the character being replaced. For example:
Execution of this function returns
"BAr"
. Following is another example:
Execution of this function returns
"AAA"
. Sometimes there is a character in the second argument string with no character at a corresponding position in the third argument string. This happens when the second argument string is longer than the third argument string. In this case, the XPath processor removes occurrences of that character.
If a character occurs more than once in the second argument string, the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, the XPath processor ignores the excess characters.
Converting Objects to Strings
In some situations, you might want to force a string comparison. The XPath processor performs a string comparison only when the operands are neither Boolean nor numeric values. If an operand is numeric or Boolean, call the
string()
function on it to convert it to a string. The format of the
string()
function is
The
string()
function can convert any object to a string. If you omit the argument, it defaults to a node set with the context node as the only member. The string value of an element node is the concatenation of the string values of all text node descendants of the element node in document order.
When the
string()
function converts a node set to a string, it returns the string value of the node in the node set that is first in document order. If the node set is empty, the
string()
function returns an empty string.
The
string()
function converts numbers to strings as follows:
- NaN (not a number) becomes
"NaN"
- Positive zero becomes
"0"
- Negative zero becomes
"0"
- Positive infinity becomes
"Infinity"
- Negative infinity becomes
"-Infinity"
- An integer becomes a sequence of digits with no leading zeros, for example,
"1234"
. A negative integer is preceded by a minus sign, for example,
"-1234"
.
- A noninteger number becomes a sequence of digits with at least one digit before a decimal point and at least one digit after a decimal point, for example,
"12.34"
. A negative noninteger number is preceded by a minus sign, for example,
"-12.34"
. Leading zeros are not allowed unless there is only one to satisfy the requirement of a zero before the decimal point. Beyond the one required digit after the decimal point, there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values.
The
string()
function converts the Boolean false value to the string
"false"
, and the Boolean true value to the string
"true"
.
Finding Strings That Start with a Particular String
To determine if a string starts with a particular string, specify the
starts-with()
function. The format is
This function returns
true
if the first argument string starts with the second argument string, and otherwise returns
false
.