Pad Between Strings
Function Syntax | (LM:PadBetween <s1> <s2> <ch> <ln>) |
Current Version | 1.0 |
Arguments | ||
---|---|---|
Symbol | Type | Description |
s1,s2 | String | Strings to be concatenated |
ch | String | Character for padding |
ln | Integer | Minimum length of returned string |
Returns | ||
Type | Description | |
String | Concatenation of strings s1,s2 padded in between to a minimum length |
Program Description
This function returns a string of a minimum specified length which is the concatenation of two supplied strings, padded between using a supplied character.
This function may be used to generate a string of a known length consisting of two elements of unknown length - such behaviour is useful when printing reports or lists to the command-line. As an example, this function is extensively utilised in my Dynamic Block Counter program, to generate the report of the block quantities at the command-line.
As expected, the iterative version of the program is approximately 2.5 times faster than the recursive version, but I have included the recursive version to provide some variation on how the result can be achieved.
Recursive Version
;;---------------------=={ Pad Between }==--------------------;; ;; ;; ;; Returns a string of a minimum specified length which is ;; ;; the concatenation of two supplied strings padded to a ;; ;; desired length using a supplied character. ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; s1,s2 - strings to be concatenated ;; ;; ch - character for padding ;; ;; ln - minimum length of returned string ;; ;;------------------------------------------------------------;; ;; Returns: Concatenation of s1,s2 padded to a min length ;; ;;------------------------------------------------------------;; (defun LM:PadBetween ( s1 s2 ch ln ) (if (< (+ (strlen s1) (strlen s2)) ln) (LM:PadBetween (strcat s1 ch) s2 ch ln) (strcat s1 s2) ) )
Iterative Version
;;---------------------=={ Pad Between }==--------------------;; ;; ;; ;; Returns a string of a minimum specified length which is ;; ;; the concatenation of two supplied strings padded to a ;; ;; desired length using a supplied character. ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; s1,s2 - strings to be concatenated ;; ;; ch - character for padding ;; ;; ln - minimum length of returned string ;; ;;------------------------------------------------------------;; ;; Returns: Concatenation of s1,s2 padded to a min length ;; ;;------------------------------------------------------------;; (defun LM:PadBetween ( s1 s2 ch ln ) ( (lambda ( a b c ) (repeat (- ln (length b) (length c)) (setq c (cons a c))) (vl-list->string (append b c)) ) (ascii ch) (vl-string->list s1) (vl-string->list s2) ) )
Example Function Call
_$ (LM:PadBetween "String1" "String2" "." 30) "String1................String2"