NCL 206: Building and Formatting Strings
So far, we've used strings as values to store, inspect, split, transform, and compare.
Sometimes we need to reshape them instead.
NCL provides operations for trimming text from the edges of strings, padding them to useful widths, inserting new text, and replacing existing text.
Trimming strings
S.TRIML repeatedly removes a string from the left side of another string.
S.TRIML <dst> <str> <trim>
For example:
#message $000
S.MOVE #message ":::Hello"
S.TRIML #message #message ":"
produces:
Hello
S.TRIMR does the same thing from the right:
S.TRIMR <dst> <str> <trim>
Using both:
S.MOVE #message ":::Hello:::"
S.TRIML #message #message ":"
S.TRIMR #message #message ":"
produces:
Hello
The trimming string can contain more than one character:
S.TRIML #message ".....@" ".."
produces:
.@
".." is removed twice. The remaining "." is not a complete match, so trimming stops.
Normalizing input
Trimming can be useful before processing input.
For example, if leading and trailing : characters are not meaningful:
:::SHOUT:Hello!:::
we can normalize the input first:
S.TRIML #input #input ":"
S.TRIMR #input #input ":"
leaving:
SHOUT:Hello!
The rest of the program can then work with a known form.
We used the same general technique in NCL 205 when we converted a command to uppercase before comparing it: sometimes it is simpler to put data into a known form before working with it.
Trimming changes the input
Trimming is only appropriate when the text being removed is not meaningful data.
If the characters could legitimately be part of the value itself, trimming them changes the data rather than merely normalizing its form.
Padding strings
Padding does the opposite: it adds text until a string reaches a requested length.
S.PADL pads the left side:
S.PADL <dst> <str> <pad> <len>
For example:
#number $000
S.MOVE #number "42"
S.PADL #number #number "0" 5
produces:
00042
S.PADR pads the right side:
S.PADR <dst> <str> <pad> <len>
#label $000
S.MOVE #label "READY"
S.PADR #label #label "." 10
produces:
READY.....
Aligning values
Padding is useful for fixed-width fields.
Suppose we want a name to occupy 10 characters and a score to occupy 6:
#name $000
#score $001
#scoreText $002
S.MOVE #name "EMMA"
MOVE #score 42
S.PADR #name #name " " 10
S.MOVE #scoreText #score
S.PADL #scoreText #scoreText " " 6
The two fields now contain:
"EMMA "
" 42"
A longer name or shorter score changes the amount of padding without changing the requested field widths.
Padding with longer strings
Like trimming, padding can use a string containing more than one character.
S.PADR #result "42" "ab" 5
produces:
42abab
The result has length 6, even though we requested 5. S.PADR adds the complete padding string until the requested length is reached or exceeded.
Padding and trimming use complete strings
Trimming only removes complete matches. With:
S.TRIML #result ".....@" ".."
one "." remains.
Padding only adds complete padding strings. With:
S.PADR #result "42" "ab" 5
the result reaches length 6.
Multi-character trimming can therefore stop with part of the pattern remaining, while multi-character padding can exceed the requested length.
When padding must produce an exact length, S.SUB can remove any excess.
-- Exact right padding: preserve the left side.
S.PADR #result "42" "ab" 5
S.SUB #result #result 0 4
-- > "42aba"
-- Exact left padding: preserve the right side.
S.PADL #result "42" "ab" 5
S.SUB #result #result -5 -1
-- > "bab42"
When right padding overshoots, preserve the requested length from the left. When left padding overshoots, preserve it from the right.
Repeating with padding
An empty string has length 0, so padding one can also produce repeated text:
#line $000
S.PADR #line "" "-" 12
produces:
------------
A longer padding string works the same way:
S.PADR #line "" ".-" 12
produces:
.-.-.-.-.-.-
Here the padding happens to fit the requested length exactly.
Inserting text
S.JOIN combines strings at their ends. S.INS places new text inside an existing string.
S.INS <dst> <str> <ins> <pos>
For example:
#message $000
S.MOVE #message "Hello!"
S.INS #message #message ", world" 5
produces:
Hello, world!
The insertion position can also come from another operation.
S.FIND #position #message #target
S.INS #message #message #insert #position
S.FIND locates the target, and S.INS inserts the new text immediately before it.
Replacing text
S.REP replaces matching text inside a string.
S.REP <dst> <str> <old> <new> <count>
For example:
#message $000
S.MOVE #message "one two two"
S.REP #message #message "two" "three" 1
produces:
one three two
The final operand controls the number and direction of replacements:
| Count | Behavior |
|---|---|
| Positive | Replace up to that many matches from the beginning |
| Negative | Replace up to that many matches from the end |
0 |
Replace every match |
So:
S.REP #message "one two two" "two" "three" -1
produces:
one two three
while:
S.REP #message "one two two" "two" "three" 0
produces:
one three three
A replacement can also be empty:
S.REP #message "one two two" "two" "" 1
produces:
one two
Only the first match is removed.
Putting the tools together
Let's turn:
NCS e READY
into:
NCS/e READY......
First, find "e " and insert / before it:
#message $000
#position $001
S.MOVE #message "NCS e READY"
S.FIND #position #message "e "
S.INS #message #message "/" #position
giving:
NCS /e READY
Now remove only the first space:
S.REP #message #message " " "" 1
giving:
NCS/e READY
Finally, pad the result to 17 characters:
S.PADR #message #message "." 17
giving:
NCS/e READY......
The complete program is:
#message $000
#position $001
S.MOVE #message "NCS e READY"
S.FIND #position #message "e "
S.INS #message #message "/" #position
S.REP #message #message " " "" 1
S.PADR #message #message "." 17
D.TXT #message
D.BLT
Try it
Create a score field from:
#name $000
#score $001
#scoreText $002
S.MOVE #name "EMMA"
MOVE #score 42
Make the final display look like:
EMMA......|...42
The name field should be 10 characters wide and the score field should be 5.
Then try:
S.MOVE #name "ALICE"
MOVE #score 7
and predict the result before running it.
For an extra challenge, build:
[ALICE]...|....7
Insert the brackets using S.INS rather than storing "[ALICE]" directly. Remember that the first insertion changes the positions that follow it.
We've learned how to remove, add, repeat, insert, and replace pieces of strings.
We've also used padding to control how values are represented as text.
In NCL 207: Number Bases, we'll look more closely at numeric representations, including binary, octal, decimal, and hexadecimal.