Padding Strings
BlitzMax Forums/BlitzMax Programming/Padding Strings
| ||
Need a string to be a certain length, padding with spaces if it is too short? Easy with slices. add [..length] to the string. So a[..20] will pad a with as many spaces needed to make a 20 characters long. a = "Hello World!"[..20] will make a equal to "Hello World! " (There should be 8 spaces after the !. The forum doesn't show that very well). code example: SuperStrict Print "a"[..20]+":0" Print "ab"[..20]+":0" Print "abc"[..20]+":0" Print "Two words"[..20]+":0" Print "Hello World!"[..20]+":0" result: a :0 ab :0 abc :0 Two words :0 Hello World! :0 |
| ||
Have you looked at the implementations of LSet and RSet? :pFunction LSet$( str$,n ) Return str[..n] End Function Function RSet$( str$,n ) Return str[Len(str)-n..] End Function |
| ||
I looked for such a command and completely overlooked LSet. Thought "there might be a way to do it with slicing" so tested it out, and sure enough, the strings were padded with spaces. |
| ||
You can also add padding to the left by slicing them with a negative index. These things work on arrays too, by the way, where the "padding" will be done with Null- or 0-Elements. |