1 / 9

Manipulating Text

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.

lorna
Download Presentation

Manipulating Text

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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!

  3. 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$

  4. 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!”

  5. 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$

  6. 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$

  7. 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$

  8. 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.

  9. 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.

More Related