Scratch Custom String Blocks

Having the ability to manipulate strings is one of the most powerful facilities you can have in a programming language. In my day to day coding I manipulate strings an awful lot. You may want to be able to get specific characters in a string, convert the whole string to uppercase characters or lowercase characters. Many years ago, one of the first programming languages I learnt was BASIC (http://en.wikipedia.org/wiki/BASIC). BASIC has several functions for allowing you to manipulate strings, for example:

LEFT$(string, n)

LEFT$ allows you to return the LEFT most n characters from your string. For example if you use the command PRINT LEFT$(“Scratch”,3) the letters Scr would be printed to the screen. The n parameter tells the function how many characters starting at the beginning of the string you want.

RIGHT$(string, n)

Similar to LEFT$, RIGHT$ allows you to return the RIGHT most n characters from your string. So something like PRINT RIGHT$(“Scratch”, 3) would print tch to the screen. The n parameter tells the function how many characters starting at the end of the string and working backwards you want.

MID$(string, start, n)

MID$ is quite a powerful function. It allows you to get a certain amount of characters, but starting at any point in the string. So using a command like PRINT MID$(“Scratch Programming”, 9, 7) would print Program to the screen. The start parameter tells the function where you want to start in the string and the n parameter tells the function how many characters you want to return from the starting point.

UPPER$ and LOWER$

Some other functions available are UPPER$ and LOWER$. These return either an uppercase or lowercase version of your string. So something like PRINT UPPER$(“scratch”) would print SCRATCH and PRINT LOWER$(“SCRATCH”) would print scratch. Again, very useful!

How can we do this in Scratch?

Out of the box, Scratch does not have these kind of blocks built in. As we know, Scratch 2 allows us to write our own custom blocks, so we could write our own versions of these. What I have done is build versions of the functions explained above as custom blocks that can be used in your Scratch programs.

I have made these available at http://scratch.mit.edu/projects/12402145/. If you look inside the project, all the custom blocks are defined on the stage, with an example of how you would use each block next to the block definition. I will given an explanation below on how you would use each custom block.

Please Note: The resulting string from each of these blocks is stored in a variable called ‘result’.

Scratch LEFT$

This has been implemented as a custom block called Left. You would use the block like this:

Scratch Left BlockThe first parameter is the string or the variable holding the string you want to work with. The second parameter is the number of characters from the left you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold Scr.

Scratch RIGHT$

This has been implemented as a custom block called Right. You would use the block like this:

Scratch Right BlockThe first parameter is the string or variable holding the string you want to work with. The second parameter is the number of characters from the right that you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold tch.

Scratch MID$

This has been implemented as a custom block called Mid. You would use the block like this:

Scratch Mid BlockWe have three parameters that we have to supply to this block. The first parameter is the string or variable holding the string you want to work with. The second parameter is where you want to start in the string. The final parameter is the number of characters you want to get from the string. The result is stored in a variable called result. So, looking at the example call above, we can see that we will be working with the string Scratch Programming and we want to start returning characters from the 9th character in the string and that we want to return 7 characters. Starting at the 9th character (P) we will get 7 characters, which will give us the string Program.

Scratch UPPER$ and LOWER$

I have also implemented blocks that allow strings to be converted to upper of lower case. These blocks are called ToUpperCase and ToLowerCase. As with all the other blocks, the result is stored in a variabled called result. You would use the blocks like this:

Scratch ToUpperCase BlockScratch ToLowerCase BlockCalling the ToUpperCase block on the string shown would convert it to SCRATCH and calling the ToLowerCase block on the string shown would convert it to scratch. If you supplied a string such as ScRaTcH to either of these blocks, it will still work as expected.

I am going to write a further blog post on how the ToUpperCase and ToLowerCase blocks work, because Scratch is a little different to most languages when comparing upper and lower case characters. The code for the other blocks is fairly easy to follow and figure out whats going on.

Anymore Blocks?

I will be implementing more string manipulation blocks over a period of time. The blocks I want to implement next are:

  – SPLIT(string, char)
  – LTRIM(string)
  – RTRIM(string)
  – TRIM(string)

SPLIT enables you to split one string into several strings, splitting the string at a specific character. For example if you supplied a string I Program In Scratch and told the block to split on the space character, you would end up with 4 strings containing:

  I
  Program
  In
  Scratch

Each of these strings would be held in a list.

LTRIM and RTRIM removes all spaces to either the left or the right of a string. So if you had a string ”    Scratch” (quotes used to show the spaces) and passed the string to the LTRIM block, it would remove all the leading spaces. Similarly, if you had the string “Scratch   ” and passed the string to the RTRIM block, it would remove all the trailing spaces. TRIM performs a union of LTRIM and RTRIM and will remove all leading and trailing spaces.

As these blocks are added, I will add a post to this blog and will also add a comment in the Scratch forums and on the ScratchEd web site.

Use and Share!

I do hope that you find these blocks useful and feel free to use and modify them in any way you would like. Share them around with your fellow Scratchers.

Advertisements

One thought on “Scratch Custom String Blocks

  1. Incredible! Quite useful, the functions. However, I had a bit of a trouble with the Split function. It only splits provided there is only one splitchar character. Two or more splitchar characters results in nothing. Is it possible for you to program me a split function that can do that? I’ll give you credit for it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s