On September 22nd I blogged about a set of custom string blocks I implemented for Scratch. These have been very well received and I am glad people are finding them useful. I did say that I was going to add some more custom blocks to manipulate strings, and I am pleased to say that I have now added four more blocks to the Scratch project. I have now implemented the following blocks:
LTRIM allows you to trim any leading spaces from a string (Left Trim). Using an example, consider the string ” Scratch” (quotes used to show the spaces). You can pass this string into the LTRIM block and it will remove all the leading spaces, giving you the string “scratch”. You use the block as follows:
RTRIM allows you to trim any trailing spaces from a string (Right Trim). Again using an example where the string is in quotes, if you call the block with the string “Scratch “ you will receive a string back with the trailing spaces removed. The block is used as follows:
TRIM is a combination of LTRIM and RTRIM and will remove any leading and trailing spaces. If you look at the block in Scratch, it shows a good example of being able to reuse exisiting blocks. All TRIM does is call the LTRIM and RTRIM blocks. As an example, if you call the block with the string ” Scratch “ all of the leading and trailing spaces are removed. You use the block as follows:
SPLIT is an extremely useful block and in the programming languages I use on a daily basis, it is implemented and I use it often. SPLIT allows you to split a string at a certain character. A simple example of this is splitting a sentence into individual strings. If we passed in the string “I like programming in Scratch” and tell it to split on spaces, the SPLIT block will split this into indiviudal strings as follows:
I very often have to write programs that work with comma seperated values (CSV). In my programs, I would read a line of data from a file that could look like “phil curnow,11/04/1972,41” – This just shows a simple line with my name, date of birth and age. Now, using my language of choice, which is C#, if I wanted to display each of these values on screen, I could write some program code that looks like this:
string str = “phil curnow,11/04/1972,41”;
foreach (string s in str.Split(‘,’))
This would print each of the three items on a seperate line on the screen. All I am saying in the program is that I want to split the string wherever there is a comma.
So how can we implement this in Scratch? Well, this is where lists come in to play. If we pass a string into our custom block and tell it to split on a space, each of the ‘sub-strings’ can be added to a list, and this is exactly how I have implemented the block.
To call our custom block, we need to supply two parameters, the string we want to split and the character we want to split on. So, looking at the example below, we will pass in the string I like programming in Scratch and the second parameter, although looks blank, is a space.
Do you want to see anymore string blocks?
We now have several very useful custom blocks to manipulate strings in our Scratch projects. Are there any other blocks you would like to see for manipulating strings? If you can think of any other blocks, leave a comment on here or drop me a tweet and time permitting, I will see if I can implement it for you.