Text functions
Text functions measure, search, take apart, combine and change texts. The entries come in this order:
-
measuring and searching:
string-length,index-of-substring; -
taking a part of a text:
substring,substring-before,substring-after; -
combining:
concat,string-join,repeat; -
changing between upper and lower case:
upper-case,lower-case,capitalize-first; -
handling white space:
normalize-space,trim,trim-left,trim-right; -
padding:
pad-left,pad-right; -
replacing:
replace,replace-regex; -
encoding:
url-encode.
Other functions that work on texts have their entries elsewhere:
The following rules apply to all text functions:
-
Positions in a text start at 1, and lengths and positions count characters.
-
Comparisons of texts are exact: upper-case and lower-case letters are different characters.
-
EFX reads the value of a text field with its white space already normalised: without white space at the start and at the end, and with each run of white-space characters inside reduced to a single space. You can still read the original value as the raw value of the field.
string-length
Returns the number of characters in a text.
string-length(<text>) → number
- <text>
-
text. The text to measure.
Every character counts as one, whatever its script. If the text has no value, the result is 0.
Examples
string-length(BT-00-Text) <= 400
Checks that a text field holds at most 400 characters.
validation/rules.efx)WITH BT-111-Lot
ASSERT string-length(BT-111-Lot) <= 6000
AS ERROR R-H1X-510
FOR BT-111-Lot
IN *;
A rule that limits the length of a text field.
index-of-substring
Returns the position at which a text first occurs inside another text.
index-of-substring(<text>, <substring>) → number
- <text>
-
text. The text to search in.
- <substring>
-
text. The text to search for.
The result is the position, counting from 1, of the first character of the first occurrence of <substring> in
<text>. If <substring> does not occur, the result is 0. If <substring> is an empty text, the result is 1. If
<text> or <substring> has no value, the result has no value.
Examples
index-of-substring('abcabc', 'ca')
Gives 3.
index-of-substring(BT-00-Text, '-') > 0
Checks that the text contains a hyphen.
| New in EFX-2. |
substring
Returns a part of a text, given its starting position and, optionally, its length.
substring(<text>, <start>) → text substring(<text>, <start>, <length>) → text
- <text>
-
text. The text to take a part from.
- <start>
-
number. The position of the first character to keep, counting from 1.
- <length>
-
number. Optional. The number of characters to keep. Without it, the part extends to the end of the text.
substring first rounds <start> and <length> to the nearest whole number, rounding a half up. The result then
contains each character whose position is at least <start> and, when you give <length>, lower than <start>
plus <length>. Positions outside the text do not count. So a start of 0 with a length of 3 keeps only the first
two characters, and a start after the end gives an empty text. If <text> has no value, the result is an empty text.
Examples
substring('2024-03-01', 6, 2)
Gives 03.
substring(BT-00-Text, 1, 3)
Keeps the first three characters of the text.
substring(BT-00-Identifier, 5)
Keeps everything from the fifth character onwards.
See also: substring-before, substring-after, subsequence
substring-before
Returns the part of a text that comes before the first occurrence of a delimiter.
substring-before(<text>, <delimiter>) → text
- <text>
-
text. The text to take a part from.
- <delimiter>
-
text. The text that marks the end of the part.
The result is the part of <text> before the first occurrence of <delimiter>. If <delimiter> does not occur in
<text>, or is an empty text, the result is an empty text. If <text> has no value, the result is an empty text.
Examples
substring-before(BT-00-Email, '@')
Gives the local part of an e-mail address: info for info@example.eu.
substring-before('2024-03-01', '-')
Gives 2024.
| New in EFX-2. |
See also: substring-after, substring, split
substring-after
Returns the part of a text that comes after the first occurrence of a delimiter.
substring-after(<text>, <delimiter>) → text
- <text>
-
text. The text to take a part from.
- <delimiter>
-
text. The text that marks the start of the part.
The result is the part of <text> after the first occurrence of <delimiter>. If <delimiter> does not occur in
<text>, the result is an empty text. If <delimiter> is an empty text, the result is the whole of <text>. If
<text> has no value, the result is an empty text.
Examples
substring-after(BT-00-Email, '@')
Gives the domain of an e-mail address: example.eu for info@example.eu.
substring-after('2024-03-01', '-')
Gives 03-01: only the first hyphen counts.
| New in EFX-2. |
See also: substring-before, substring, split
concat
Joins texts into one text.
concat(<text>) → text concat(<text>, <text>, …) → text
- <text>
-
text. A text to join.
concatjoins the texts in the order given, without any separator.
The result is a text. With a single argument, the result is that text. An argument that has no value counts as an empty text. To join values of other types, convert them first with text. To join the values of a sequence, use string-join.
Restrictions
-
Every argument must be a text. A number or another type of value as argument is an error.
Examples
concat(BT-00-Code, '-', BT-00-Identifier)
concat('Lot ', text(BT-00-Integer), ' of ', text(count(BT-13-Text)))
Builds a text such as Lot 2 of 5.
See also: string-join, text
string-join
Joins the texts of a sequence into one text, with a separator between them.
string-join(<sequence>, <separator>) → text
- <sequence>
-
sequence of text values. The texts to join, in the order of the sequence.
- <separator>
-
text. The text to put between two values that follow each other. It can be an empty text.
If the sequence is empty, the result is an empty text. If the sequence has a single value, the result is that value, without separator.
Restrictions
-
The sequence must contain text values. To join numbers or other values, convert each of them with text, for example in an iteration.
Examples
string-join(BT-13-Text, ', ')
Lists the values of the repeatable field BT-13-Text, separated by a comma and a space.
string-join(distinct-values(BT-13-Text), ' / ')
Lists each value only once.
string-join(for number:$n in BT-13-Number return text($n), '; ')
Converts each number to text before joining.
| New in EFX-2. |
repeat
Repeats a text a given number of times.
repeat(<text>, <count>) → text
- <text>
-
text. The text to repeat.
- <count>
-
number. A whole number: how many times to repeat the text.
The result is <text> repeated <count> times, without separator. If <count> is 0 or negative, the result is an
empty text. If <text> has no value, the result has no value.
Restrictions
-
<count>must be a whole number.
Examples
repeat('-', 10)
Gives a line of ten hyphens.
repeat('*', count(BT-13-Text))
Gives one star for each value of the repeatable field BT-13-Text.
| New in EFX-2. |
upper-case
Converts a text to upper case.
upper-case(<text>) → text
- <text>
-
text. The text to convert.
upper-case replaces each lower-case letter with its upper-case form, as Unicode defines it. The conversion does not
depend on a language, and it can change the length of the text: straße becomes STRASSE. Characters that have no
upper-case form stay as they are. If the text has no value, the result is an empty text.
Examples
upper-case(BT-00-Code)
upper-case(BT-00-Text) == upper-case(BT-12-Text)
Compares two texts, whether their letters are in upper or lower case.
| New in EFX-2. |
See also: lower-case, capitalize-first
lower-case
Converts a text to lower case.
lower-case(<text>) → text
- <text>
-
text. The text to convert.
lower-case replaces each upper-case letter with its lower-case form, as Unicode defines it. The conversion does not
depend on a language. Characters that have no lower-case form stay as they are. If the text has no value, the result
is an empty text.
Examples
lower-case(BT-00-Email)
ends-with(lower-case(BT-00-Url), '.pdf')
Checks the end of an address, whether its letters are in upper or lower case.
| New in EFX-2. |
See also: upper-case, capitalize-first
capitalize-first
Converts the first character of a text to upper case.
capitalize-first(<text>) → text
- <text>
-
text. The text to convert.
capitalize-first converts the first character in the same way as
upper-case, and leaves the rest of the text as it is.
An empty text gives an empty text. If the text has no value, the result has no value.
Examples
capitalize-first('élan')
Gives Élan.
capitalize-first(lower-case(BT-00-Text))
Writes the text in lower case, except for its first letter.
| New in EFX-2. |
See also: upper-case, lower-case
normalize-space
Removes the white space at the start and at the end of a text, and replaces each run of white space inside it with a single space.
normalize-space(<text>) → text
- <text>
-
text. The text whose white space the function normalises.
The white-space characters are the space, the tab, the line feed and the carriage return. Other characters that look like spaces, such as the non-breaking space, stay. If the text has no value, the result is an empty text.
EFX already reads the values of text fields in this way. So the function is useful mainly with raw values, and with texts that other functions build.
Examples
normalize-space(BT-00-Text:rawValue)
normalize-space(concat(BT-00-Text, ' ', BT-12-Text))
Joins two texts with a space. When one of them is empty, no double space and no space at the end remain.
| New in EFX-2. |
trim
Removes the white space at the start and at the end of a text.
trim(<text>) → text
- <text>
-
text. The text to trim.
The white-space characters are the space, the tab, the line feed and the carriage return. Unlike normalize-space, the function does not change white space inside the text. If the text has no value, the result is an empty text.
Examples
trim(BT-00-Text:rawValue)
string-length(trim(BT-00-Text:rawValue)) == string-length(BT-00-Text:rawValue)
Checks that the raw value has no white space at its start or at its end.
| New in EFX-2. |
See also: trim-left, trim-right, normalize-space
trim-left
Removes the white space at the start of a text.
trim-left(<text>) → text
- <text>
-
text. The text to trim.
The function removes the spaces, tabs, line feeds and carriage returns at the start of the text, and leaves the rest as it is. If the text has no value, the result is an empty text.
Examples
trim-left(BT-00-Text:rawValue)
| New in EFX-2. |
See also: trim, trim-right
trim-right
Removes the white space at the end of a text.
trim-right(<text>) → text
- <text>
-
text. The text to trim.
The function removes the spaces, tabs, line feeds and carriage returns at the end of the text, and leaves the rest as it is. If the text has no value, the result is an empty text.
Examples
trim-right(BT-00-Text:rawValue)
| New in EFX-2. |
pad-left
Adds characters at the start of a text until it reaches a given length.
pad-left(<text>, <length>, <padding>) → text
- <text>
-
text. The text to pad.
- <length>
-
number. A whole number: the length to reach.
- <padding>
-
text. The text to add, usually a single character such as
'0'or' '.
pad-left repeats <padding> in front of <text> until the result has <length> characters. When <padding> has
several characters, the function cuts the repeated padding so that the result has exactly <length> characters. A
text that already has <length> characters or more is returned unchanged: the function never shortens it. An empty
<padding> leaves the text unchanged. If <text> has no value, the result has no value.
Restrictions
-
<length>must be a whole number.
Examples
pad-left('42', 5, '0')
Gives 00042.
pad-left(text(BT-00-Integer), 5, '0')
Writes an integer with at least five digits.
| New in EFX-2. |
pad-right
Adds characters at the end of a text until it reaches a given length.
pad-right(<text>, <length>, <padding>) → text
- <text>
-
text. The text to pad.
- <length>
-
number. A whole number: the length to reach.
- <padding>
-
text. The text to add, usually a single character such as
'.'or' '.
pad-right repeats <padding> after <text> until the result has <length> characters. When <padding> has
several characters, the function cuts the repeated padding so that the result has exactly <length> characters. A
text that already has <length> characters or more is returned unchanged: the function never shortens it. An empty
<padding> leaves the text unchanged. If <text> has no value, the result has no value.
Restrictions
-
<length>must be a whole number.
Examples
pad-right('Lot', 8, '.')
Gives Lot......
pad-right(BT-00-Code, 10, ' ')
| New in EFX-2. |
replace
Replaces every occurrence of a text with another text.
replace(<text>, <search>, <replacement>) → text
- <text>
-
text. The text in which to make the replacements.
- <search>
-
text. The text to look for.
replacelooks for it exactly, character by character: no character has a special meaning. - <replacement>
-
text. The text to put in place of each occurrence.
replaceinserts it as it is.
replace finds the occurrences of <search> from left to right, and they do not overlap: replacing aa with x in
aaa gives xa. If <search> is an empty text, the function returns the text unchanged. A <replacement> without value counts
as an empty text, so the function removes the occurrences. If <text> or <search> has no value, the result has no
value.
To replace parts of a text that match a pattern, use replace-regex.
Examples
replace(BT-00-Text, '.', ',')
Replaces every full stop with a comma.
replace(BT-00-Identifier, 'ORG-', '')
Removes the prefix ORG- wherever it occurs.
| New in EFX-2. |
See also: replace-regex, substring-after
replace-regex
Replaces every part of a text that matches a regular expression.
replace-regex(<text>, <pattern>, <replacement>) → text
- <text>
-
text. The text in which to make the replacements.
- <pattern>
-
text. A regular expression, written as described in Regular expressions.
- <replacement>
-
text. The text to put in place of each match. It can refer to the groups of the pattern.
replace-regex finds the matches of <pattern> from left to right, and they do not overlap. It replaces each match
with <replacement>, in which:
-
$followed by a digit inserts the part of the match that the group with that number captured:$1for the first group,$2for the second, and so on.$0inserts the whole match. A group that did not take part in the match inserts nothing. -
\$inserts a dollar sign, and\\inserts a backslash.
Any other use of $ or of a backslash in <replacement> causes an error when the expression is evaluated.
If <text> has no value, the result is an empty text.
Restrictions
-
<pattern>must use only the parts of EFX regular expressions. When<pattern>is a text literal, the translator checks it when it translates the expression, and reports a part that EFX does not accept as an error. The translator cannot check a pattern given in another way, for example in a field or a variable. -
<pattern>cannot match an empty text. A pattern such as'[0-9]*', which can match zero characters, causes an error when the expression is evaluated. Use'[0-9]+'instead.
Examples
replace-regex(BT-00-Text, '[0-9]+', '#')
Replaces each run of digits with #: a1b22 becomes a#b#.
replace-regex(BT-00-Text, '([0-9]{4})/([0-9]{2})', '$2.$1')
Uses two groups to rewrite 2024/03 as 03.2024.
replace-regex(BT-00-Text:rawValue, '[ \t]+', ' ')
Replaces each run of spaces and tabs with a single space.
| New in EFX-2. |
See also: Regular expressions,
replace,
Pattern matching (like)
url-encode
Encodes a text so that it can be used as a part of a web address (URL).
url-encode(<text>) → text
- <text>
-
text. The text to encode.
The function keeps the letters A to Z and a to z, the digits 0 to 9, the hyphen (-), the underscore
(_), the full stop (.) and the tilde (~). It replaces every other character with its UTF-8 code, written as
% followed by two hexadecimal digits for each byte. For example, a space becomes %20, a slash %2F, and é
becomes %C3%A9. If the text has no value, the result is an empty text.
Examples
concat('https://example.eu/search?q=', url-encode(BT-00-Text))
Builds a web address with the text as a query parameter.
| New in EFX-2. |
See also: concat