Next: , Previous: , Up: srfi strings spec   [Index]


2.8.3.11 Reverse and append

Function: string-reverse str
Function: string-reverse str start
Function: string-reverse str start end

Reverse the string; return the result string and does not alter its str argument.

Examples:

(string-reverse "Able was I ere I saw elba.")
⇒ ".able was I ere I saw elbA"

;;; In-place rotate-left, the Bell Labs way:
(lambda (s i)
  (let ([i (modulo i (string-length s))])
    (string-reverse! s 0 i)
    (string-reverse! s i)
    (string-reverse! s)))

Unicode note Reversing a string simply reverses the sequence of code–points it contains. So a zero–width accent character a coming after a base character b in string str would come out before b in the reversed result.

Function: string-reverse! str
Function: string-reverse! str start
Function: string-reverse! str start end

In–place side–effecting variant.

Function: string-append str ...

Return a newly allocated string whose characters form the concatenation of the given strings.

Function: string-concatenate string-list

Append the elements of string-list together into a single string. Guaranteed to return a freshly allocated string.

Note that the (apply string-append string-list) idiom is not robust for long lists of strings, as some Scheme implementations limit the number of arguments that may be passed to an n–ary procedure.

Function: string-concatenate/shared string-list
Function: string-append/shared str ...

These two procedures are variants of string-concatenate and string-append that are permitted to return results that share storage with their parameters. In particular, if string-append/shared is applied to just one argument, it may return exactly that argument, whereas string-append is required to allocate a fresh string.

Function: string-concatenate-reverse string-list
Function: string-concatenate-reverse string-list final-string
Function: string-concatenate-reverse string-list final-string end

With no optional arguments, this function is equivalent to:

(string-concatenate (reverse string-list))

If the optional argument final-string is specified, it is consed onto the beginning of string-list before performing the list-reverse and string-concatenate operations.

If the optional argument end is given, only the first end characters of final-string are added to the string list, thus producing:

(string-concatenate
  (reverse (cons (substring/shared final-string 0 end)
                 string-list)))

Example:

(string-concatenate-reverse '(" must be" "Hello, I")
                            " going.XXXX"
                            7)
⇒ "Hello, I must be going."

This procedure is useful in the construction of procedures that accumulate character data into lists of string buffers, and wish to convert the accumulated data into a single string when done.

Unicode note Reversing a string simply reverses the sequence of code–points it contains. So a zero–width accent character ac coming after a base character bc in string str would come out before bc in the reversed result.

Function: string-concatenate-reverse/shared string-list
Function: string-concatenate-reverse/shared string-list final-string
Function: string-concatenate-reverse/shared string-list final-string end

Variant which is permitted to return results that share storage with its arguments.


Next: , Previous: , Up: srfi strings spec   [Index]