If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, August 20, 2012

Some extra useful String functions (XQuery)

module namespace string = "http://www.nxp.com/string";

(:
  string:capitalize("test")  -->  "Test" 
:)
declare function string:capitalize($string as xs:string) as xs:string {
    let $tokens := string:split($string)
    return concat(upper-case($tokens[1]), string-join(subsequence($tokens, 2), ''))
};

(:
    string:capitalizeAll("makes you wonder")  --> "Makes You Wonder"
:)
declare function string:capitalizeAll($string as xs:string) as xs:string {
   string-join(for $word in string:splitWords($string) return string:capitalize($word), ' ') 
};

(:
   string:split("work") --> ("w", "o", "r", "k")
:)
declare function string:split($string as xs:string) as xs:string* {
    for $codepoint in string-to-codepoints($string) return codepoints-to-string($codepoint)
};

(:
   string:splitWords("go live")  --> ("go", "live")
:)
declare function string:splitWords($string as xs:string) as xs:string* {
    tokenize($string, "\s+")
};

(: 
  string:toCamelCase("business segment") --> "businessSegment" 
:)
declare function string:toCamelCase($string as xs:string) as xs:string {
    let $words := string:splitWords($string)
    return string-join(($words[1], for $word in subsequence($words, 2) return string:capitalize($word)), '')
};

No comments:

Post a Comment