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


6.5 Cutting and Pasting Strings

procedure: string-append string …

Returns a newly allocated string made from the concatenation of the given strings. With no arguments, string-append returns the empty string ("").

(string-append)                         ⇒  ""
(string-append "*" "ace" "*")           ⇒  "*ace*"
(string-append "" "" "")                ⇒  ""
(eq? str (string-append str))           ⇒  #f ; newly allocated
procedure: substring string start end

Returns a newly allocated string formed from the characters of string beginning with index start (inclusive) and ending with end (exclusive).

(substring "" 0 0)              ⇒ ""
(substring "arduous" 2 5)       ⇒ "duo"
(substring "arduous" 2 8)       error→ 8 not in correct range

(define (string-copy s)
  (substring s 0 (string-length s)))
procedure: string-head string end

Returns a newly allocated copy of the initial substring of string, up to but excluding end. It could have been defined by:

(define (string-head string end)
  (substring string 0 end))
procedure: string-tail string start

Returns a newly allocated copy of the final substring of string, starting at index start and going to the end of string. It could have been defined by:

(define (string-tail string start)
  (substring string start (string-length string)))

(string-tail "uncommon" 2)      ⇒  "common"
procedure: string-pad-left string k [char]
procedure: string-pad-right string k [char]

These procedures return a newly allocated string created by padding string out to length k, using char. If char is not given, it defaults to #\space. If k is less than the length of string, the resulting string is a truncated form of string. string-pad-left adds padding characters or truncates from the beginning of the string (lowest indices), while string-pad-right does so at the end of the string (highest indices).

(string-pad-left "hello" 4)             ⇒  "ello"
(string-pad-left "hello" 8)             ⇒  "   hello"
(string-pad-left "hello" 8 #\*)         ⇒  "***hello"
(string-pad-right "hello" 4)            ⇒  "hell"
(string-pad-right "hello" 8)            ⇒  "hello   "
procedure: string-trim string [char-set]
procedure: string-trim-left string [char-set]
procedure: string-trim-right string [char-set]

Returns a newly allocated string created by removing all characters that are not in char-set from: (string-trim) both ends of string; (string-trim-left) the beginning of string; or (string-trim-right) the end of string. Char-set defaults to char-set:not-whitespace.

(string-trim "  in the end  ")          ⇒  "in the end"
(string-trim "              ")          ⇒  ""
(string-trim "100th" char-set:numeric)  ⇒  "100"
(string-trim-left "-.-+-=-" (char-set #\+))
                                        ⇒  "+-=-"
(string-trim "but (+ x y) is" (char-set #\( #\)))
                                        ⇒  "(+ x y)"

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