The length of a string can be determined using the string-length
function. It accepts a single string and returns its length as an integer. Whitespace is significant, so leading and trailing whitespace characters are counted. Table 17-5 shows some examples.
USEFUL FUNCTION: set-string-to-length
The set-string-to-length
function, shown here, pads a string to a desired length:
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) };
The function accepts as arguments a string, a character to pad the string with, and the desired length of the string. For example, you might call this function with:
functx:set-string-to-length("abc", "*", 7)
which returns abc****
. This function truncates $stringToPad
if it is longer than $length
.
18.116.60.158