Next: , Previous: , Up: Library   [Contents][Index]


5.4 String Manipulation Functions

Built-in Function: string escape (string str, [string chars])

Returns a copy of str with the characters from chars escaped, i.e. prefixed with a backslash. If chars is not specified, ‘\"’ is assumed.

escape('"a\tstr"ing') ⇒ '\"a\\tstr\"ing'
escape('new "value"', '\" ') ⇒ 'new\ \"value\"'
Built-in Function: string unescape (string str)

Performs the reverse to ‘escape’, i.e. removes any prefix backslash characters.

unescape('a \"quoted\" string') ⇒ 'a "quoted" string'
Built-in Function: string unescape (string str, [string chars])
Built-in Function: string domainpart (string str)

Returns the domain part of str, if it is a valid email address, otherwise returns str itself.

domainpart("gray") ⇒ "gray"
domainpart("gray@gnu.org.ua") ⇒ "gnu.org.ua"
Built-in Function: number index (string s, string t)
Built-in Function: number index (string s, string t, number start)

Returns the index of the first occurrence of the string t in the string s, or -1 if t is not present.

index("string of rings", "ring") ⇒ 2

Optional argument start, if supplied, indicates the position in string where to start searching.

index("string of rings", "ring", 3) ⇒ 10

To find the last occurrence of a substring, use the function rindex (see rindex).

Built-in Function: number interval (string str)

Converts str, which should be a valid time interval specification (see time interval specification), to seconds.

Built-in Function: number length (string str)

Returns the length of the string str in bytes.

length("string") ⇒ 6
Built-in Function: string dequote (string str)

Removes ‘<’ and ‘>’ surrounding str. If str is not enclosed by angle brackets or these are unbalanced, the argument is returned unchanged:

dequote("<root@gnu.org.ua>") ⇒ "root@gnu.org.ua"
dequote("root@gnu.org.ua") ⇒ "root@gnu.org.ua"
dequote("there>") ⇒ "there>"
Built-in Function: string localpart (string str)

Returns the local part of str if it is a valid email address, otherwise returns str unchanged.

localpart("gray") ⇒ "gray"
localpart("gray@gnu.org.ua") ⇒ "gray"
Built-in Function: string replstr (string s, number n)

Replicate a string, i.e. return a string, consisting of s repeated n times:

replstr("12", 3) ⇒ "121212"
Built-in Function: string revstr (string s)

Returns the string composed of the characters from s in reversed order:

revstr("foobar") ⇒ "raboof"
Built-in Function: number rindex (string s, string t)
Built-in Function: number rindex (string s, string t, number start)

Returns the index of the last occurrence of the string t in the string s, or -1 if t is not present.

rindex("string of rings", "ring") ⇒ 10

Optional argument start, if supplied, indicates the position in string where to start searching. E.g.:

rindex("string of rings", "ring", 10) ⇒ 2

See also String manipulation.

Built-in Function: string substr (string str, number start)
Built-in Function: string substr (string str, number start, number length)

Returns the at most length-character substring of str starting at start. If length is omitted, the rest of str is used.

If length is greater than the actual length of the string, the e_range exception is signalled.

substr("mailfrom", 4) ⇒ "from"
substr("mailfrom", 4, 2) ⇒ "fr"
Built-in Function: string substring (string str, number start, number end)

Returns a substring of str between offsets start and end, inclusive. Negative end means offset from the end of the string. In other words, yo obtain a substring from start to the end of the string, use substring(str, start, -1):

substring("mailfrom", 0, 3) ⇒ "mail"
substring("mailfrom", 2, 5) ⇒ "ilfr"
substring("mailfrom", 4, -1) ⇒ "from"
substring("mailfrom", 4, length("mailfrom") - 1) ⇒ "from"
substring("mailfrom", 4, -2) ⇒ "fro"

This function signals e_range exception if either start or end are outside the string length.

Built-in Function: string tolower (string str)

Returns a copy of the string str, with all the upper-case characters translated to their corresponding lower-case counterparts. Non-alphabetic characters are left unchanged.

tolower("MAIL") ⇒ "mail"
Built-in Function: string toupper (string str)

Returns a copy of the string str, with all the lower-case characters translated to their corresponding upper-case counterparts. Non-alphabetic characters are left unchanged.

toupper("mail") ⇒ "MAIL"
Built-in Function: string ltrim (string str[, string cset)

Returns a copy of the input string str with any leading characters present in cset removed. If the latter is not given, white space is removed (spaces, tabs, newlines, carriage returns, and line feeds).

ltrim("  a string") ⇒ "a string"
ltrim("089", "0") ⇒ "89"

Note the last example. It shows how ltrim can be used to convert decimal numbers in string representation that begins with ‘0’. Normally such strings will be treated as representing octal numbers. If they are indeed decimal, use ltrim to strip off the leading zeros, e.g.:

set dayofyear ltrim(strftime('%j', time()), "0")
Built-in Function: string rtrim (string str[, string cset)

Returns a copy of the input string str with any trailing characters present in cset removed. If the latter is not given, white space is removed (spaces, tabs, newlines, carriage returns, and line feeds).

Built-in Function: number vercmp (string a, string b)

Compares two strings as mailfromd version numbers. The result is negative if b precedes a, zero if they refer to the same version, and positive if b follows a:

vercmp("5.0", "5.1") ⇒ 1
vercmp("4.4", "4.3") ⇒ -1
vercmp("4.3.1", "4.3") ⇒ -1
vercmp("8.0", "8.0") ⇒ 0
Library Function: string sa_format_score (number code, number prec)

Format code as a floating-point number with prec decimal digits:

sa_format_score(5000, 3) ⇒ "5.000"

This function is convenient for formatting SpamAssassin scores for use in message headers and textual reports. It is defined in module sa.mfl.

See SpamAssassin, for examples of its use.

Library Function: string sa_format_report_header (string text)

Format a SpamAssassin report text in order to include it in a RFC 822 header. This function selects the score listing from text, and prefixes each line with ‘* ’. Its result looks like:

*  0.2 NO_REAL_NAME           From: does not include a real name
*  0.1 HTML_MESSAGE           BODY: HTML included in message

See SpamAssassin, for examples of its use.

Library Function: string strip_domain_part (string domain, number n)

Returns at most n last components of the domain name domain. If n is 0 the function returns domain.

This function is defined in the module strip_domain_part.mfl (see Modules).

Examples:

require strip_domain_part
strip_domain_part("puszcza.gnu.org.ua", 2) ⇒ "org.ua"
strip_domain_part("puszcza.gnu.org.ua", 0) ⇒ "puszcza.gnu.org.ua"
Library Function: string verp_extract_user (string email, string domain)

If email is a valid VERP-style email address for domain, that corresponds to a valid local user name (see validuser), this function returns the local user name, corresponding to that email. Otherwise, it returns empty string.

For example, assuming the local user ‘gray’ exists:

verp_extract_user("gray=gnu.org.ua@tuhs.org", 'gnu\..*')
  ⇒ "gray"

Next: , Previous: , Up: Library   [Contents][Index]