- [Craig] In this video, we explore the use of basic string manipulation.
(uplifting piano jingle)
So, string manipulation is the act of manipulating, extracting or changing the characters in a string variable.
Let's have a look at a few real applications of where you might use this.
So, in our first example, let's consider an online form.
So you can see here we've got a first name field and a last name field.
And the user is of course able to type in their name in any format they like.
They may just accidentally be slipping on a shift key or caps lock.
Here I've exaggerated the situation where you can see the name Craig Sargent
has been entered with a mixture of lower and uppercase characters.
We may require internally that the first letter of each name is capital and the rest are then lowercase,
regardless of how it's been typed in.
So we would need to perform some string manipulation on the contents that have come in.
We would need to extract the first letter from the entered strings and convert it to uppercase.
We need to extract all the remaining letters from the strings, convert them to lower case,
and then join or concatenate the two parts back together again.
In this example, we're looking at a typical search engine and considering something called text parsing.
You might be used to being able to type in phrases such as six centimetres in inches
and then the result appearing on the screen.
Well, six centimetres in inches would be a string,
and the search engine would then have to parse the string and manipulate it to provide a sensible output.
It would have to be able to extract the number six from the string, extract the character centimetres,
extract the character inches and then run some algorithm to understand what needs to be done.
Consider the situation of a train ticket.
In this example, train station names have to be abbreviated to a set number of characters so they can fit on a printed ticket.
With long train station names, for example London Paddington,
the string would need to be manipulated so we end up with London P-A-D-D-I-N-G-T-N.
And again, this would be an example of string manipulation.
So let's have a look at a range of string manipulation commands that you need to be aware of for the exam.
Now, the syntax being shown here is the one that you'll be presented with in the exam,
so it's the one you should become familiar with.
It might not exactly match the one that you're using in your language.
So the first is the string length command, here dot length,
and it returns the length of a string.
So we'd say variable becomes equal to string dot length,
and if string was hello, then length would become five.
We need the ability to convert strings to upper and lower case,
and we do this with dot upper and dot lower.
So we have variable equals string dot upper and variable equals string dot lower.
And in both cases, if the string supplied was the word hello,
it would be returned entirely in upper or lower case as appropriate.
The next one is dot substring.
This returns part of the string starting at the character of the first parameter
and up to the number of characters in the second parameter.
So we have string dot substring, starting position, comma, number of characters.
So string dot substring three comma one,
if the string supplied was hello, it would extract the letter L.
We can also extract part of a string by using the dot left and dot right commands.
This returns the left-most or right-most characters from a string
where the parameter indicates how many characters you want to return.
It's also possible to concatenate together or join together separate strings,
and for this, we use the addition or plus operator.
We have conversion, and we have for this the ASC command and the CHR command.
So the ASC written in capital letters would return the ASCII value of the character supplied in brackets,
and the CHR command would return the character from the ASCII number supplied in brackets.
So ASC open brackets string A would return the number 65,
whereas the other way around, CHR brackets 65 brackets,
would return the uppercase character A.
If you've been learning to program during your GCSEs in Python
then all of these commands are supported, but the syntax is a little different.
Now, the syntax we're showing here is not the one that will be presented in exam questions,
but you are allowed to answer all exam questions in the language of your choice,
and in most cases, it's very similar.
So dot length instead becomes len, L-E-N,
and then open brackets and the string you want to produce the length of.
Dot upper and dot lower operate in very much the same way.
The substring command is subtly different.
So here, we provide a string and then we use square brackets,
and we provide two parameters separated by a colon.
The first parameter indicates the starting character of the string,
and this is zero-indexed,
so the number two here would actually be the third character in the string.
And then the second parameter is the end of the string you want to return, non-inclusively.
So if our string was hello then
string, open square brackets, two, colon, three, close square brackets
would return L.
And then the ASCII conversion is very similar
apart from it's lowercase characters in Python and not uppercase,
and it's ORD to return the ASCII value, not ASC.
Otherwise, it performs exactly the same.
(uplifting piano jingle)