This [[Module Library|XQuery Module]] contains functions for string operations and computations.
=Conventions=
All functions and errors in this module and errors are assigned to the <code><nowiki>http://basex.org/modules/stringsstring</nowiki></code> namespace, which is statically bound to the {{Code|stringsstring}} prefix.<br/>
=FunctionsComputations=
==stringsstring:levenshtein==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|strings<pre>string:levenshtein|( $string1 as xs:string, $string2 as xs:string|) as xs:double}}<br/pre>|-valign="top"
| '''Summary'''
|Computes the [https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance Damerau-Levenshtein Distance] for two strings and returns a double value ({{Code|0.0}} - {{Code|1.0}}). The distance returned value is computed as follows:<br/>* <code>1.0 - </code> – distance / max(length of strings)</code>
* <code>1.0</code> is returned if the strings are equal; <code>0.0</code> is returned if the strings are too different.
|-valign="top"
| '''Examples'''
|* {{Code|stringsstring:levenshtein("cafeflower", "coffeeflower")}} returns {{Code|1}}* {{Code|string:levenshtein("flower", "lewes")}} returns {{Code|0.335}}* In the following query, the input is first normalized (words are stemmed, converted to lower case, and diacritics are removed).It returns {{Code|1}}:<pre lang='xquery'>let $norm := ft:normalize(?, map { 'stemming': true() })return string:levenshtein($norm("HOUSES"), $norm("house"))</pre>
|}
==stringsstring:soundex==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|strings<pre>string:soundex|( $string as xs:string|) as xs:string}}<br/pre>|-valign="top"
| '''Summary'''
|Computes the [https://en.wikipedia.org/wiki/Soundex Soundex] value for the specified string. The algorithm can be used to find and index English words with similar pronouncation.
|-valign="top"| '''Examples'''|* <code>string:soundex("Michael")</code> returns {{Code|M240}}* <code>string:soundex("OBrien") = string:soundex("O'Brien")</code> returns {{Code|true}}|} ==string:cologne-phonetic== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>string:cologne-phonetic( $string as xs:string) as xs:string</pre>|- valign="top"| '''Summary'''|Computes the [https://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik Kölner Phonetik] value for the specified string. Similar to Soundex, the algorithm is used to find similarly pronounced words, but for the German language. As the first returned digit can be {{Code|0}}, the value is returned as string.|- valign="top"
| '''Examples'''
|
* <code>stringsstring:soundexcologne-phonetic("Michael")</code> returns {{Code|M240645}}.* <code>strings:soundexevery $s in ("OBrienMayr", "Maier", "Meier") = stringssatisfies string:soundexcologne-phonetic($s) = "O'Brien67")</code> returns {{Code|true}}.
|}
=Formatting=strings ==string:cologne-phoneticformat==
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|strings<pre>string:cologne-phonetic|format( $string pattern as xs:string|, $values... as item()) as xs:string}}<br/pre>|-valign="top"
| '''Summary'''
|Computes Returns a formatted string. The remaining {{Code|$values}} are incorported into the {{Code|$pattern}}, according to [https://dedocs.wikipediaoracle.orgcom/javase/8/docs/api/java/wikiutil/K%C3%B6lner_Phonetik Kölner PhonetikFormatter.html#syntax Java’s printf syntax] value for the specified string. |- valign="top"| '''Errors'''|{{Error|format|#Errors}} The algorithm was published by Hans Joachim Postel in 1969. Similar to Soundex, it specified format is used to find similarly pronounced words in the German languagenot valid.|-valign="top"
| '''Examples'''
|
* <code>strings{{Code|string:cologne-phoneticformat("%b", true())}} returns {{Code|true}}.* {{Code|string:format("Michael%06d", 256)</code> }} returns {{Code|645000256}}.* <code>strings{{Code|string:format("%e", 1234.5678)}} returns {{Code|1.234568e+03}}.|} ==string:colognecr== {| width='100%'|-phoneticvalign="top"| width='120' | '''Signature'''|{{Code|'''string:cr()''' as xs:string}}|- valign="Mayrtop"| '''Summary'''|Returns a single carriage return character ({{Code|&#13;}}) .|} = strings=string:colognenl== {| width='100%'|-phoneticvalign="top"| width='120' | '''Signature'''|{{Code|'''string:nl()''' as xs:string}}|- valign="Meiertop"| '''Summary'''|Returns a single newline character ({{Code|&#10;}})</code> returns .|} ==string:tab== {| width='100%'|- valign="top"| width='120' | '''Signature'''|{{Code|'''string:tab()''' as xs:string}}|- valign="top"| '''Summary'''|Returns a single tabulator character ({{Code|true&#9;}}).
|}
=Changelog=
;Version 10.0* Updated: Renamed from ''Strings Module'' to ''String Module''. The namespace URI has been updated as well.* Updated: {{Function||string:format}}, {{Function||string:cr}}, {{Function||string:nl}} and {{Function||string:tab}} adopted from the obsolete Output Module was introduced with Version 8.3.
[[Category:XQuery]]The Module was introduced with Version 8.3. Functions were adopted from the obsolete Utility and Output Modules.