Transcript for:
String Manipulation Techniques in Delphi

in this video we're going to be looking at how to manipulate strings in dely and this is part one of the video series and we're going to be looking particularly how to manipulate strings using some built-in functions that you find in dely now before we get to that let's have a look at what a string is now first of all a string in delphy is some sort of text it can have numbers in it but it's considered text so we're going to be using like numbers letters and referring to that so let's say we've got a string called St temp it's not you can call your strings whatever you want I tend to call mine something beginning with an S and then the name purely so that I know that be referring to a string so s temp so it tells me that s tells me that it's a string and there you can see I've made it equal to hello world of assign to the value of hello world so basically what dely does is it breaks that string into different blocks and each block is its own character with its own asky value so as you can see in the position one we have the H for hello world and then position two the E and so on and so on so this string takes up 11 places of text um or of characters so they're the different individual characters that we've got so if I want to refer to just one of those characters you can use square brackets after the name of the string and then the number that you put in that block will be the character that you're referring to so if I refer in this case St temp Square braet 5 that will return just the letter O and if I say s temp square bracket s or six sorry that will return just the charact or the space as you can see there's a space over there so although there's nothing in there you can't see anything it actually is a character it is a space so let's just try that quickly we've got a little program here when I click on the button called demo I've got my string that's already got hello world in it being assigned to it I'm simply going to use the show message which takes in a string and I'm so going to say s temp square bracket 5 and hopefully it will display just the O as a message that pops up let's have a look so if I click on the button there's just the O for the fifth character inside the string hello world so let's get stuck into these functions that we want to use to help us manipulate strings now just I'm going to use this hello world in St as my basis for my examples so there I've just put it there so you can see what hello world looks like and where the position of each character is now remembering what functions do is they return some sort of value or result they will return one piece of information back to you so when you are using functions you normally use it in when you have a variable equals the function because that value needs to be returned into something or you use it in some some sort of control that's going to be display something or something like that so remember a string fun or functions any function doesn't get called just by itself it normally has something equal to it or something like that so let's have a look at our first one we're going to deal particularly with string functions now we call them string functions but the ones we're going to do handle strings but they're going to return an integer they're going to return a number so that's what we want we're going to give it a string and it's going to return a number and this is going to help us to manipulate strings later on so let's have a look at the first example the length function now the length function will return how long the string is so there we've got the format you can see there's an integer variable normally that is going to be assigned the value of the length where we have the name of the function and that in Brackets is followed by the string that we want to find the length of so that will always return an integer value because you obviously as I said you can't have a a string with 5.3 characters it's going to have five characters or six characters we can't have a3d of a character so that's going to return how long that that set of text is so if we had to use it for example this in this case we have an integer called I num and we say it equals to the length of s temp then in this case s temp as you can see there has 11 characters so the result that will be returned to UNAM will be be 11 let's try this in IND Delia to see how it works so here we have our program again with the demo button we've got s temp equal to hello world and now we've got a integer variable called AR num and RAM we set to the length of St temp of that variable and then I display it using a show message now the reason why I have to do this conversion is because as I as remember what we said I num gets returned as an integer and we can't display integers in show message we can only display strings so we need to converge it from an integer to a string so let's run it and see what it looks like when we press demo the answer is 11 and that is correct because hello world has 11 characters the next integer function that we're going to learn about is pause now pause as is with all these functions returns an integer and in this case it's going to return an integer but it takes inside it two parameters both of them must be of type strength so it's going to still return an integer value now what pause does is there's two strings now I've called the first string a substring because what it does is it takes whatever the first string is and it looks for its position inside the second string so it looks to see if this string is contained inside of that string and if it does it Returns what is the position or the number that that string starts at so if I have this example let's say we got I num equal equals the PA of w r capital w r inside of s temp it's going to look for that set of of characters inside estamp and if we look there there we go there's a capital w r and although it goes from 7 to 9 it starts at position 7 so therefore I num will equal to 7 cuz that's where it starts if we asked for a small w o r it would not find it because W capital W and a small W are different string values they're different asky value so therefore they're not the same if I asked for that it would return a zero because that is not contained anywhere in this string so yeah we've got pause and we're looking for the capital w r inside of s temp we're looking for it inside there and whatever that result will be returned to our num and then we display our num so let's run it and see what it does it should be a seven and there we go it is a seven now if let's test that theory about it the capital W and the small W being the same if I make that a small W you see it doesn't look the same they're completely different um s key values for those two characters so it should not be considered the same thing so if I run it although it seems like it's got the same thing if I say demo it's going to return a zero because that string is contained nowhere inside of that string so there you've got the functions that return integers we've got length which returns how long a string is and then pause which takes in two strings and it looks for where the first string is contained inside the second string and Returns the position where it is in that string the next functions that we're going to deal with we're going to still use this St temp equal hello world but the next set of string functions that we're going to deal with don't return integers but they return another string and the first one that we are going to look at is the copy and this is quite a nice one and you use it quite often to extract parts of the string that you want and you copy it and you put it into another variable so the normal format is it we take a normal string variable and it equals to copy now copy takes in three parameters now listen carefully to how they work the first parameter is a string parameter and that is the string that you want to copy from then we've got these two numbers and people often get mixed up with what these numbers mean the first number is where do you want to start copying what position do you want to start copying so if I say a one it will start copying from there if I say copy from position five it'll start copying from there including that character the next one now don't get confused it's not from this one to this one that's not what it's saying the F the first integer is where we start copying the second integer is how many characters you want to copy so if I say I want to copy from position three for four characters it's not saying from three to the fourth character it's saying copy from position three for four characters so it'll be three 4 5 6 cuz that is four characters from position three so there's an example so copy from St starting at position three for five characters will be starting at the L over there and there'll be another L the o a space and the W is at 1 2 3 4 five characters if I have this scenario where we copy from position five for three characters that means we'll start at position five copy for three characters so it'll be the five the space and then the seven and then if I put something absurd like that let's say we copy from position 9 for a th000 characters well you'll see we can only go up until 11 but it won't give an error it will simply copy what it can so it'll just copy the N9 the 10 and the 11 and stop right there so here we have that example we're going to take s temp I've created another variable of type of string which is s word and s word is going to equal copy from the S1 variable from position three for five characters it's going to take that result and return it to s word it's not not going to change St temp it's just going to take the result and put it into s word and then we just show what s word's going to look like so if we run it there you can see it's l l o space w those are the five characters starting at position three so if I wanted to extract just for example the first five letters so I can just get the low part I can say copy from position one for five characters so it'll copy from there for five characters and just have the word hello so if I run that that'll say just the word hello and if I wanted to get just the part that says World well that's starting a position six there's seven so that's the seventh character so we start at position seven and we're going to copy for one 2 3 4 five characters as well so C start at position 7 copy for five characters and there we have the world or just world there's no other the last function that we're going to learn today that also returns a string is quite easy there two versions of it we're going to do both versions quite quickly and that is the uppercase and lowercase functions and it's very simple they just take in a string and whatever the text is it'll change it all to uppercase whether it's uppercase already or not or whether it's all lowcase it doesn't matter every single letter will be changed to uppercase so for example we're going to have a string variable is equal or assign the value of uppercase and then in Brackets we put the string that we want to convert so if you have something like this then every single letter inside this string will be uppercase in other words capital h capital e capital l capital l o and so on the lower case works exactly the same way except for it converts every character into its lowercase version so it works exactly the same like upper case and it would give the same result except for it would be a small H and then all all of this would be exactly the same and that would be a small W so yeah we're going to test it out we've got our St temp I've got our s word is going to equal to uppercase of stamp we're not going to change estamp we're simply taking whatever estamp is converting it to uppercase and storing that in a brand new variable that's how functions work so if I run it there you can see Hello World all in uppercase and if I change that word upper to a lower then you'll see that all the text will be in lowercase now there we go so just to recap the functions that return strings we've got copy so a string V it returns a string so a string variable is equal to copy then it takes in the string that you want to copy from then the first number is where you want to start copying what the position where you want to start copying the second integer is how many characters you want to copy and then the last string function that we learned was the uppercase and the lower case which just simply takes a string as parameter and stores the result in a new string in either uppercase form or lowercase in capitals or small letters in our next video in this video series we'll be looking at string procedures you can go find that at our YouTube channel there's the address over there you can find us on Facebook or on Twitter follow us we'd love to hear your comments also you can find out whenever we upload new videos and you can go to our website particularly for it videos there it's Mr long. wix.com / longr it looks something like this if I get to it there we go it looks something like that we're hopefully going to have lots of videos for all grades there so go have a look hopefully you can find something interesting and remember don't do it the long way do it the Mr long way