pad-string

Name

pad-string -- Pads a string, in front, to the specified length

Synopsis

(pad-string string length padchar)

Description

Returns string, padded in front with padchar to at least length Returns string unmodified if string is not a (string?), padchar is not a (string?), padchar is the empty string, or if string is already greater than or equal to length in length.

string

The string to pad.

length

The desired length.

padchar

The character (string, actually) to use as padding. If padchar is longer than 1 character, the resulting string may be longer than length when returned.

Author

Norman Walsh, <ndw@nwalsh.com>

Source Code

(define (pad-string string length padchar)
  ;; Pads a string, in front, to the specified length
  (if (and (string? string) 
	   (string? padchar) 
	   (> (string-length padchar) 0))
      (let loop ((s string) (count (- length (string-length string))))
	(if (<= count 0)
	    s
	    (loop (string-append padchar s) 
		  (- count (string-length padchar)))))
      string))