90 likes | 208 Vues
In this lesson, we explore the importance of text manipulation and how to use basic functions to process strings effectively. We cover techniques such as concatenation to combine names, calculating string length for validation, and extracting characters from various parts of a string — the beginning, end, and middle. Additionally, we discuss finding specific characters within strings. By the end of the lesson, you'll know how to dissect full names into forenames and surnames, utilizing functions like left$(), right$(), and mid() to enhance user interaction.
E N D
Manipulating Text In today’s lesson we will look at: • why we might want to pick out parts of text strings • some BASIC functions that can be used to chop up text
Why Process Text? • There might be occasions when a particular piece of information can be presented in different ways. • Think about a person’s name – you might want to display: • the whole name – e.g. Andrew Virnuls • just the forename – e.g. Andrew • initial and surname – e.g. A Virnuls • However, your user wouldn’t be very happy if you asked them for all the possible versions of their name!
Concatenation • The simplest thing you can do with text strings is join them together – this is called concatenation • We looked at this in lesson 2 – we can use the + operator to join words, e.g. forename$ = “Andrew” surname$ = “Virnuls” fullname$ = forename$ + “ “ + surname$
String Length • Sometimes you need to calculate the length of a string – e.g. to make sure that it will fit on the screen, or to check that it has the right number of characters (e.g. a bank sort code has 6 digits) • There is a function called len() that tells you how long a string is, e.g. input “Please enter your sort code: ”; sortcode$ chars = len(sortcode$) if chars <> 6 then print “That doesn’t look right!”
Characters from the Start • Sometimes you might want to take part of the string from the left hand end. • You can do this using the left$() function – you give it the string and the number of characters you want, e.g. input “What is your name? ”; forename$ initial$ = left$(forename$, 1) print “Your first initial is ”; initial$
Characters from the End • There is a corresponding function called right$(), which gives you characters from the end of the string. • For example... input “What is your name? ”; forename$ last$ = right$(forename$, 1) print “Your name ends with ”; last$
Characters from the Middle • Should you want to take some characters from the middle of a string, there is a function called mid$() • You tell it the string, where you want to start, and the number of characters, for example... input “What is your name? ”; fore$ middle$ = mid$(fore$, 2, len(fore$)-2) print “The middle of your name is ”; middle$
Finding a Character • If you want to know if your string contains a particular character, you can use instr() • You give it a string and a character, and it returns the position of the first occurrence of the character within the string, for example: a$ = "hello world" print instr(a$, " ") • If the character doesn’t appear in the string, the output of instr() is 0.
Putting It All Together • What code could you use to separate a full name into a forename and a surname? input "What is your full name? "; fullname$ space = instr(fullname$, " ") forename$ = left$(fullname$, space) surname$ = right$(fullname$, len(fullname$)-space) print "Your forename is "; forename$ print "Your surname is "; surname$ • Using right$() can sometimes be a bit tricky because you are counting characters backwards from the end of the string.