Download presentation
Presentation is loading. Please wait.
Published byMatthew Flowers Modified over 9 years ago
1
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
2
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! Why Process Text?
3
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$ Concatenation
4
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!” String Length
5
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 Start
6
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 End
7
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$ Characters from the Middle
8
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. Finding a Character
9
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. Putting It All Together
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.