Returns the portion of the value of $sourceString
beginning at the position
indicated by the value of $start
and continuing for the number of $length
.
fn:substring
( $sourceString
as xs:string?
,$start
as xs:double
xs:string
fn:substring
( $sourceString
as xs:string?
,$start
as xs:double
,$length
as xs:double
xs:string
If the value of $sourceString
is the empty sequence, the function returns
the zero-length string.
Otherwise, the function returns a string comprising those characters of $sourceString
whose index position (counting
from one) is greater than or equal to the value of $start
(rounded to an
integer), and (if $length
is specified) less than the sum of
$start
and $length
(both rounded to integers).
The characters returned do not extend beyond $sourceString
. If
$start
is zero or negative, only those characters in positions greater
than zero are returned.
More specifically, the three argument version of the function returns the characters in
$sourceString
whose position $p
satisfies:
fn:round($start) <= $p and $p < fn:round($start) + fn:round($length)
The two argument version of the function assumes that $length
is infinite
and thus returns the characters in
$sourceString
whose position $p
satisfies:
fn:round($start) <= $p
In the above computations, the rules for op:numeric-less-than
and
op:numeric-greater-than
apply.
The expression fn:substring("motor car", 6)
returns " car"
.
The expression fn:substring("metadata", 4, 3)
returns "ada"
.
The expression fn:substring("12345", 1.5, 2.6)
returns "234"
.
The expression fn:substring("12345", 0, 3)
returns "12"
.
The expression fn:substring("12345", 5, -3)
returns ""
.
The expression fn:substring("12345", -3, 5)
returns "1"
.
The expression fn:substring("12345", 0 div 0E0, 3)
returns ""
.
The expression fn:substring("12345", 1, 0 div 0E0)
returns ""
.
The expression fn:substring((), 1, 3)
returns ""
.
The expression fn:substring("12345", -42, 1 div 0E0)
returns "12345"
.
The expression fn:substring("12345", -1 div 0E0, 1 div 0E0)
returns ""
.
The first character of a string is located at position 1, not position 0.
The second and third arguments allow xs:double
values (rather than
requiring xs:integer
) in order to achieve compatibility with XPath 1.0.
A surrogate pair counts as one character, not two.
The consequences of supplying values such as NaN
or positive or negative
infinity for the $start
or $length
arguments follow from the
above rules, and are not always intuitive.