Programming Lesson 3 - Variables and Data Types

Strings and Characters

Introduction

So far, we have only been able to store and manipulate numbers. Sooner or later, you will need to make a program which can work with text. It doesn’t seem so obvious now, but you’d be surprised how often you need to take a piece of text, chop it up, add bits on to it, count the letters and generally jiggle it about a bit. The rules for text are simple:

  • Any SINGLE letter, number or symbol is called a Character
  • Two or more letters, numbers or symbols are called a String

You simply create variables of type Sting or Char as needed, in the same way you did for number values.

Task 1 – Basics

  1. Create a new project, call it “strings”
  2. Add a Label for output and a Command Button to your form. Give each an appropriate name.
  3. Double click your button to program it
  4. Create a new variable called sName, set the type as string and set its default value to your SURNAME
  5. Program your button so the contents of Name is printed out to the label on your form.

Extension: Modify the program so the value of the string can be entered by the user when the program is run

Task 2 – Adding Characters

  1. Add a new variable underneath your string, call it initial, set the type as Character and the default value as your initial.
  2. Change your code so you now output your whole name to the label when the button is clicked.

Problems:

How did you output both pieces of information on the same label?

How did it look??

Extension: Modify the output so there is a full stop after the initial, then a space, then your Surname

Further Extension: Modify the program so it asks the user for their initial and surname as separate pieces of information. Create a new variable called “FullName” and assign their full, formatted name to this variable before outputting to the label.

Task 3 – Concatenation

String concatenation simply means combining string values. To do this we use the AMPERSAND symbol &.

Clear all code from your button sub routine before continuing.

  1. Add text boxes and labels to your form so that the user can enter their first name, last name and their student number
  2. When the command button is clicked, the user should see a greeting such as the one below. This should be achieved using concatenation. Your have free choice of variables used and their names:

“Hello FULL NAME, it’s nice to see you have a student number of XXXXXX”

  1. Extension – Add to your output the LENGTH of the persons name.

Task 4 – String Manipulation

Sometimes, we only want to take a bit of a string and use it. For example, your user name consists of your Initial, the first 4 letters of your surname and then your student number.

To do this in Visual Basic, we need to use some string manipulation key words that live in the STRINGS class:

Strings.Left()

Strings.Right()

Strings.Mid()

You should have enough basic knowledge to be able to complete the following tasks:

  1. Start a new project, call it “UserNameGenerator”
  2. Create an interface that has:
  3. Text boxes to enter a Forename, Surname and Student Number
  4. A Label to output the username
  5. A label to output the email address
  6. Labels that explain the function of each part of your interface
  7. A command button to make everything happen
  8. Make your program generate a username and email address for the users details.

Extension:

The user name format has changed, it’s now:

  • The users initial, last three letters of the surname, last letter of the forename and then student number. Change your program to reflect this.
  • What if we decided we only wanted the initial, 2nd, 3rd and 4th letters of the surname and then student number? Change your program to reflect this.

What about suggesting a suitably secure password for the user, using randomly selected characters from a string you specify?

Can you add error catching? If a user doesn’t fill in a textbox, for example, can you spot this using an IF statement?

Final note about numbers in strings

You can do this either in VB or simply by looking at the code and working out what it does.

Consider the following code:

Dim number1 AsString = "4"

Dim number2 AsString = "6"

Dim answer AsString

answer = number1 + number2

What do you think will be stored in the variable “answer” when this program is run?

Write down the reason why this happens:

Task 5 – Stop typing in gibberish

Virtually every program requires input from the user, usually through a text box. The problem with users is that they can be silly and type in things you really don’t want. For example, ask them for their age and they’ll type in the name of their cat.

To stop this from happening, you need to be able to apply some validation. Validation is simply a set of rules that any input must follow if it is to be accepted. We will look in depth at this when you have learned how to make decisions using the IF statement.

In this task, make a simple program with a text box for input, a label for output and a button.

Add code so that whenever the user enters text it is automatically converted to lower case.

Task 6 – VB Saves the day (and you a shed load of time and energy)

There is something called the “string class” in VB. What this means is when you make a string variable, it automatically knows how to do a load of incredibly useful things.

How do you access these features?

Simple, declare a string, then type its name and press . and select the action you want to perform

Example:

In the above example, we could find out if a string contains a certain word or not.

  1. Create a program which has:
  2. Two text boxes
  3. txtSentence – this will hold a sentence the user types in
  4. txtWord – this will hold a word to find/remove/replace
  5. A button
  6. A label for output

Code your program to do the following:

  1. Output the length of the sentence entered
  2. Output whether the sentence does or does not contain a word entered
  3. Output the sentence with a word removed – the word will be entered in txtWord
  4. Output the sentence with a word replaced – use input boxes if necessary to do this

Extension

Add code to your program so that it outputs a list of individual words in the sentence. Hint, you are SPLITTING the sentence…

© Mr Davidson 2015