NCL 203: Looking Inside Strings
In NCL 202, we learned how to change strings and combine them to create new ones.
Sometimes, though, we don't need to change a string.
We need to learn something about it.
For example, suppose we have:
#message "Hello!"
How many characters does that string contain?
Finding the length
S.LEN finds the length of a string:
S.LEN <dst> <str>
For example:
#length $000
#message "Hello!"
S.LEN #length #message
D.TXT #length
D.BLT
The program displays:
6
S.LEN examines the string and stores its length in #length.
That length can immediately be used by the integer operations we already know.
Using the length
For example:
#length $000
#message "Hello!"
#limit 10
S.LEN #length #message
BGT :long #length #limit
D.TXT "Short"
JUMP :done
:long
D.TXT "Long"
:done
D.BLT
The program displays:
Short
S.LEN stores 6 in #length.
Then:
BGT :long #length #limit
interprets that value as an integer and asks whether it is greater than 10.
It isn't, so execution continues downward and displays "Short".
Try changing:
#message "Hello!"
to:
#message "This is a considerably longer message."
Before running the program, predict which path it will take.
The program isn't making a decision about a number we wrote directly into an instruction. It is making a decision using information it discovered about a string.
The pattern should look familiar:
- Examine a value.
- Store the result.
- Compare the result.
- Choose a path.
We've done the same thing with calculations such as MOD. Now we're applying that pattern to text.
The empty string
A string does not have to contain any characters at all.
This:
""
is an empty string.
The two quotation marks mark the beginning and end of the string, but there is nothing between them.
Its length is therefore zero:
#length $000
#message ""
S.LEN #length #message
D.TXT #length
D.BLT
The program displays:
0
That gives us a simple way to check whether a string is empty:
#length $000
#message ""
S.LEN #length #message
BEQ :empty #length 0
D.TXT "Not empty"
JUMP :done
:empty
D.TXT "Empty"
:done
D.BLT
Change #message to different strings and predict which path the program will take.
Finding text inside a string
Length tells us how many characters a string contains.
Sometimes we want to know where some text appears inside it.
For that, NCL provides S.FIND:
S.FIND <dst> <haystack> <needle>
For example:
#position $000
#message "Hello, world!"
#search "world"
S.FIND #position #message #search
D.TXT #position
D.BLT
The program displays:
7
S.FIND searches #message for the string in #search.
If it finds a match, it stores the position where that match begins in #position.
String positions
Positions within a string begin at 0.
For "Hello, world!", the positions look like this:
| Character | H |
e |
l |
l |
o |
, |
|
w |
o |
r |
l |
d |
! |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Position | 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
| From end | -13 |
-12 |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
The first character, H, is at position 0.
Why does the first position start at 0?
Zero-based positions are extremely common in programming.
One useful way to think about a position is as an offset from the beginning of something.
The first character is zero characters away from the beginning, so its offset is 0. The next character is one character away, so its offset is 1, and so on.
NCL follows that same convention.
The w in "world" is at position 7.
Positions can also be counted backward from the end of a string. The final character is at position -1, the character before it is at -2, and so on.
We won't need negative positions yet, but they'll become useful when we start taking strings apart in NCL 204.
For now, S.FIND gives us positions counted from the beginning.
So:
S.FIND #position #message #search
stores:
7
in #position.
When nothing is found
What happens if the string we're looking for isn't there?
Try:
#position $000
#message "Hello, world!"
#search "goodbye"
S.FIND #position #message #search
D.TXT #position
D.BLT
The program displays:
-1
S.FIND returns -1 when it cannot find the requested string.
That gives us a useful distinction:
S.FIND result |
Meaning |
|---|---|
0 or greater |
Found, beginning at that position |
-1 |
Not found |
We can use that result to choose a path.
Does a string contain something?
For example:
#position $000
#message "Hello, world!"
#search "world"
S.FIND #position #message #search
BEQ :missing #position -1
D.TXT "Found"
JUMP :done
:missing
D.TXT "Not found"
:done
D.BLT
S.FIND finds "world" at position 7.
Since 7 is not -1, the branch is not taken and the program displays:
Found
Change:
#search "world"
to:
#search "goodbye"
Now S.FIND returns -1, the branch is taken, and the program displays:
Not found
This lets the program answer a useful question:
Does this string contain this other string anywhere?
But S.FIND gives us more information than simply yes or no.
It tells us where the match occurred.
Does a string start with something?
Consider:
#position $000
#message "NCS/e READY"
#search "NCS/e"
S.FIND #position #message #search
S.FIND returns 0.
Since position 0 is the beginning of a string, we now know that #message starts with "NCS/e".
We can test for that directly:
#position $000
#message "NCS/e READY"
#search "NCS/e"
S.FIND #position #message #search
BEQ :starts #position 0
D.TXT "Does not start with "
D.TXT #search
JUMP :done
:starts
D.TXT "Starts with "
D.TXT #search
:done
D.BLT
The program displays:
Starts with NCS/e
Now change the message:
#message "READY: NCS/e"
S.FIND still finds "NCS/e", but this time it finds it at position 7.
The string contains "NCS/e", but does not start with it.
And if we use:
#message "READY"
S.FIND returns -1 because "NCS/e" does not appear at all.
| Message | S.FIND result |
Contains NCS/e? |
Starts with NCS/e? |
|---|---|---|---|
NCS/e READY |
0 |
Yes | Yes |
READY: NCS/e |
7 |
Yes | No |
READY |
-1 |
No | No |
The same S.FIND result can therefore answer different questions depending on how we use it.
Combining what we know
We can combine S.LEN, S.FIND, and the control flow we learned in the 100-level lessons to classify a string in several ways:
#length $000
#position $001
#message "Hello from NCS/e"
#search "NCS"
S.LEN #length #message
BEQ :empty #length 0
S.FIND #position #message #search
BEQ :starts #position 0
BEQ :missing #position -1
D.TXT "Contains "
D.TXT #search
JUMP :done
:starts
D.TXT "Starts with "
D.TXT #search
JUMP :done
:missing
D.TXT "No match"
JUMP :done
:empty
D.TXT "Empty"
:done
D.BLT
Follow the program from the beginning.
First:
S.LEN #length #message
BEQ :empty #length 0
checks whether the message is empty.
It isn't, so the program continues.
Then:
S.FIND #position #message #search
searches for "NCS".
For the current message, the result is greater than 0.
That means:
BEQ :starts #position 0
is not taken, and:
BEQ :missing #position -1
is not taken either.
Execution continues downward and displays:
Contains NCS
Try changing #message to:
#message "NCS/e READY"
then:
#message "Hello!"
and finally:
#message ""
Before running each version, follow the branches and predict which message will be displayed.
Together, those examples exercise all four paths through the program.
Try it: ignoring capitalization
S.FIND distinguishes between uppercase and lowercase letters.
Consider:
#position $000
#message $001
#search $002
S.MOVE #message "Hello from NCS/e"
S.MOVE #search "ncs/E"
S.FIND #position #message #search
D.TXT #position
D.BLT
The capitalization of "NCS/e" and "ncs/E" does not match, so the search fails.
But we already know how to change the case of a string.
Modify the program so that the search works regardless of how the letters in #message and #search are capitalized.
You can use either S.UPR or S.LWR.
Remember that both strings need to be converted to the same case before S.FIND compares them.
Once your program works, try changing the capitalization of either original string:
S.MOVE #message "HELLO FROM ncs/E"
S.MOVE #search "NcS/e"
The search should still succeed.
For an extra challenge, modify the program so that the strings stored in #message and #search remain unchanged.
You'll need two more memory locations to hold the converted versions.
By combining operations we've already learned, we've made a case-insensitive search without needing a new search instruction.
S.FIND can tell us where part of a string begins.
In the next lesson, NCL 204: Taking Strings Apart, we'll use positions like these to start extracting pieces from strings.