2.12 (aux string)

Operating on strings.

2.12.1 Procedures

Procedure: break string regex

Break the string wherever regex occurs.

Specification:

string (string): String with interspersed breaking patterns (see the example).

regex (regex string pattern): Pattern that indicates the places where the string needs to be broken (see the example).

Return value (list of strings): The parts resulting after breaking the string where the regex matches. The breaking patterns are also included.

If not matches are found in the given string, the return value is a list with a single item, the original string: (list string).

Example:

> (break «Hello ~PERSON_A~, I am ~PERSON_B~.» «~[A-Z_]+~»)
(«Hello » «~PERSON_A~» «, I am » «~PERSON_B~» «.»)
Procedure: delete-pos string i

Return a copy of string minus the character in position i.

Specification:

string (string): See the example.

i (natural): Natural number corresponding to an index of the string.

Return value (string): New string based on string, but without the character in position i.

If the string is empty, an empty string is returned. If the position i does not exist, the original string is returned.

Example:

> (delete-pos «balanche» 6)
«balance»
Procedure: empty? string

Return true if the string is empty (its length is 0).

Specification:

string (string): See examples.

Return value (boolean): True if the string is empty; false otherwise.

Examples:

> (empty? «»)
#t
> (empty? «train»)
#f
Procedure: equalize-length strings

Make all strings equal in length by adding spaces to the shorter ones.

Specification:

strings (list of strings): See the example.

Return value (list of strings): New list with strings equal in length. If strings is an empty list, an empty list is returned.

Example:

> (equalize-length
   (list «Wood:»
         «Iron:»
         «Total items:»))
(«Wood:       »
 «Iron:       »
 «Total items:»)
Procedure: first string

Return the first character of the string.

Specification:

string (string): See the example.

Return value (character or empty string): If the string is not empty, return the first character. Otherwise, return an empty string.

Example:

> (first «Lollipop»)
#\L
Procedure: insert string-a string-b i

Insert string-a into string-b at position i.

Specification:

string-a (string): See example.

string-b (string): See example.

i (natural): Natural number corresponding to an index of string-b.

Return value (string): The expanded string. If the position i does not exist, string-a is appended to string-b.

Example:

> (insert «mmo» «math» 2)
«mammoth»
Procedure: last string

Return the last character of string.

Specification:

string (string): See the example.

Return value (character or empty string): If the string is not empty, return the last character. Otherwise, return an empty string.

Example:

> (last «Lollipop»)
#\p
Procedure: longest strings

Return the longest string in strings.

Specification:

strings (list of strings): See the example.

Return value (string): When there is a string longer than the rest, the longest string is returned. When two or more strings are the longest, the one with the lowest index is returned. Finaly, if the list is empty, an empty string is returned.

Example:

> (longest (list «Marte» «Saturno» «Mercurio»))
«Mercurio»
Procedure: substitute old new string

Substitute the old substring with the new substring in the string.

Specification:

old (regex string pattern): See the example.

new (string): See the example.

string (string): See the example.

Return value (string): New string with all the ocurrences of old text replaced by the new text.

Example:

> (substitute «~[A-Z]+~» «Jane Roe» «Ms. ~PERSON~ is called ~PERSON~ for a reason.»)
«Ms. Jane Roe is called Jane Roe for a reason.»