String Wrap

Function Syntax (LM:StringWrap <str> <len>)
Current Version 1.0
Arguments
Symbol Type Description
str String String to wrap to a specific length
len Integer Maximum length of each substring
Returns
Type Description
List List of substrings of specified length or less

Program Description

This subfunction will break a supplied string at spaces (if possible) into a list of substrings, each of a specified length or less (depending on the position of spaces in the original string).

This functionality may be useful when displaying strings of an unknown length in a frame with a known fixed width.

Select all
;;---------------------=={ String Wrap }==--------------------;;
;;                                                            ;;
;;  Breaks a string at spaces (if possible) into a list of    ;;
;;  substrings of a specified length or less.                 ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright  2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - String to wrap to a specific length                 ;;
;;  len - Maximum length of each substring                    ;;
;;------------------------------------------------------------;;
;;  Returns:  List of substrings of specified length or less  ;;
;;------------------------------------------------------------;;

(defun LM:StringWrap ( str len / pos )
    (if (< len (strlen str))
        (cons
            (substr str 1
                (cond
                    (   (setq pos (vl-string-position 32 (substr str 1 len) nil t)))
                    (   (setq pos (1- len)) len)
                )
            )
            (LM:StringWrap (substr str (+ 2 pos)) len)
        )
        (list str)
    )
)

Example Function Call

_$ (LM:StringWrap "This is a very long string which needs to be wrapped." 20)

("This is a very long" "string which needs" "to be wrapped.")

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010