substring Function
Returns the substring of the first argument starting at the position specified in the second argument and the length specified in the third argument.
string substring(string, number, number?)
Remarks
Each character in the string is considered to have a numeric position: the position of the first character is 1, the position of the second character is 2, and so on.
If the third argument is not specified, it returns the substring starting at the position specified in the second argument and continuing to the end of the string.
The following function call returns "234":
substring("12345",2,3)
The following function call returns "2345":
substring("12345",2)
The returned substring contains those characters for which the position of the character is greater than or equal to the rounded value of the second argument, and if the third argument is specified, less than the sum of the rounded value of the second argument and the rounded value of the third argument. The comparisons and addition used for the preceding follow the standard IEEE 754 rules; rounding is done as if by a call to the round()
function.
The following examples illustrate unusual cases.
substring("12345", 1.5, 2.6)
returns "234
"
substring("12345", 0, 3)
returns "12
"
substring("12345", 0 div 0, 3)
returns""
substring("12345", 1, 0 div 0)
returns""
substring("12345", -42, 1 div 0)
returns"12345"
substring("12345", -1 div 0, 1 div 0)
returns""
Example
This example demonstrates the preceding substring()
expressions.
XML File
None; the XSLT file calls itself.
XSLT File (substring.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> substring("12345",2,3) = <xsl:value-of select='substring("12345",2,3)'/><br/> substring("12345",2) = <xsl:value-of select='substring("12345",2)'/><br/>substring("12345", 1.5, 2.6)
= <xsl:value-of select='substring("12345", 1.5, 2.6)
'/><br/>substring("12345", 0, 3)
= <xsl:value-of select='substring("12345", 0, 3)
'/><br/>substring("12345", 0 div 0, 3)
= <xsl:value-of select='substring("12345", 0 div 0, 3)
'/><br/>substring("12345", 1, 0 div 0)
= <xsl:value-of select='substring("12345", 1, 0 div 0)
'/><br/>substring("12345", -42, 1 div 0)
= <xsl:value-of select='substring("12345", -42, 1 div 0)
'/><br/>substring("12345", -1 div 0, 1 div 0)
= <xsl:value-of select='substring("12345", -1 div 0, 1 div 0)
'/> </xsl:template> </xsl:stylesheet>
Try It
- Follow the instructions in Initiate XSLT in a Script
- Use the XSLT style sheet above (substring.xsl) as the input for both the XML and XSLT files.
- Run the script. You should get the following output.
Formatted Output
substring("12345",2,3) = 234
substring("12345",2) = 2345
substring("12345", 1.5, 2.6) = 234
substring("12345", 0, 3) = 12
substring("12345", 0 div 0, 3) =
substring("12345", 1, 0 div 0) =
substring("12345", -42, 1 div 0) = 12345
substring("12345", -1 div 0, 1 div 0) =
Processor Output
<?xml version="1.0" ?> substring("12345",2,3) = 234<br /> substring("12345",2) = 2345<br /> substring("12345", 1.5, 2.6) = 234<br /> substring("12345", 0, 3) = 12<br /> substring("12345", 0 div 0, 3) = <br /> substring("12345", 1, 0 div 0) = <br /> substring("12345", -42, 1 div 0) = 12345<br /> substring("12345", -1 div 0, 1 div 0) =
See Also
Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference