home contribute faq download

FunctX XQuery Functions

functx:pad-string-to-length

Pads a string to a desired length

Google
Webdatypic.com

Description

The functx:pad-string-to-length function returns $stringToPad appended with enough repetitions of $padChar to make its length $length. The function will truncate $stringToPad if its length is greater than $length.

Arguments and Return Type

NameTypeDescription
$stringToPad xs:string? the string to pad
$padChar xs:string the character(s) to use as padding
$length xs:integer the desired length
return value xs:string

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:pad-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)
 } ;

Examples

XQuery ExampleResults
functx:pad-string-to-length('abc', '*', 6)
abc***
functx:pad-string-to-length('abcdef', '*', 4)
abcd
functx:pad-string-to-length('', '*', 4)
****

See Also

functx:pad-integer-to-lengthPads an integer to a desired length by adding leading zeros
functx:repeat-stringRepeats a string a given number of times

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, https://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery