s LearnSqueak.com

ABSOLUTE BEGINNER

Your First Prefix Test in Squeak

Ask if a piece of text starts with a few letters, in five tiny Workspace steps.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Ask if the start matches

In Workspace, select the whole line and choose Print it. Squeak checks only the start.

Select the whole expression, then Print it
'Squeak' beginsWith: 'Squ'
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Ask if the start matches gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The first three letters match, so the answer is true.

STEP 2

Find a word in the middle

Shell appears later. This question does not search the entire text.

Select the whole expression, then Print it
'sea shell' beginsWith: 'shell'
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Find a word in the middle gives false
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The answer is false because the text starts with sea.

STEP 3

Start with the first word

Change the text after the colon to the first word.

Select the whole expression, then Print it
'sea shell' beginsWith: 'sea'
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Start with the first word gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The answer changes to true.

STEP 4

Change the capital letter

Capital S and small s are different letters for this check.

Select the whole expression, then Print it
'Squeak' beginsWith: 'squ'
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Change the capital letter gives false
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The answer is false.

STEP 5

Match the whole word

The starting text can be as long as the whole string.

Select the whole expression, then Print it
'tea' beginsWith: 'tea'
NATIVE SQUEAK CAPTURE
Native Squeak Workspace: Match the whole word gives true
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The exact match also answers true.

YOU DID IT

A small step. A real result.

beginsWith: answers true only when the requested text is at the start. It compares case exactly.

Optional code download: open it as text and copy one example into Workspace.

Download the five examples; copy one at a time

Your first text messages · More Smalltalk messages