A-level Computing/AQA/Paper 1/Fundamentals of programming/String-handling

String handling functions
Very popular examination questions involve manipulating strings. These simple functions will help you with this task.

Length
This function is used to find the length of any string you pass it, counting all the characters, including the spaces. In visual basic to find the length of a string we use the  function that returns the integer length of the string that it has been passed:

22

Position
This function allows us to find the position of an item within a given string and returns the position's location. In visual basic this is performed by the following command: For example we might want to find the location of an end of a sentence by looking for a fullstop:

23

We can also use this command to search for strings within strings. For example if we were to look for to see if a sentence contained a certain name:

25

If the search item is not contained in the string then it will return 0

0

Substring
This function allows you to snip items out of a string and return a substring. Visual Basic uses the following command: . For example we might want to find the local number from a landline phone number we have been given. We'll have to ignore the area code:

567890

Concatenation
This function allows you to stick strings together (concatenate) so that you can start to build strings using variables. Visual Basic uses the following command: For example we might have a users name stored in a variable  and a greeting that we would like to give them:

Hello Charles. How are you today?

String conversion functions
When you declare a variable you give it a datatype. This datatype restricts the values that you can place into the variable. For example:
 * would allow:
 * would NOT allow:
 * would NOT allow:

This seems to make sense, but what would happen when you try to place a real number into a integer:

34

This might seem OK, but in other languages we might run into trouble. To perform this we would have to convert from one datatype to another:

34.3 34

Write a short program to tell someone how many letters they have in their name (just in case they don't know!), for example:

Input: Fremlin

Hello Fremlin, you have 7 letters in your name.

Some people have stupidly typed their firstname and their surname into a database, write some code to display the first name, then their surname

Input: Elizabeth Sheerin

Firstname: Elizabeth

Surname: Sheerin

A telephone number has been typed into a computer as a string: (01234)567890 Write some code to output the number without brackets:

Input: (01234)567890

Output: 01234567890

A similar question to the one above, telephone numbers are currently stored in a very unreadable format: 01234567890, completely missing off the area code. Can you convert them to display the first 5 figures are the area code: This should then be output as:

Input: 01234567890

Output: (01234)567890

A palindrome is a word, phrase or number that may be read the same way in either direction. For example 1234321, RACECAR, TOOT and NUN. You need to write a program that checks to see if any input given is a palindrome and let the user know:

Input: NUN

That is a palindrome!

Input: nune

That is NOT a palindrome

You will often want to check the format of a string being input and if it is incorrect you will want it to be submitted again. For example you might want someone to input the name of their best friend, meaning that they shouldn't be inputting any letters or spaces, and it should start with a capital letter:

Name of best friend: Beanie(CORRECT)

Name of best friend: jonny5(STOP THIS)

To do this we can match the input string against some rules, regular expressions or regex, in this case we only want characters from the alphabet:

Breaking apart the rule: Another example might be checking for the correct spelling of a famous composer: "Handel", "Händel", and "Haendel" We can check this using the pattern. Let's take a look at what this means:
 * - start exactly one instance of a capital letter
 * - followed by as many lower case letters as you like (that's what the  means)
 * - start with an
 * - includes an  or (the   symbol) an   followed by an optional   (  means the e is optional)

Most regular expression tools provide the following operations to construct expressions.

Boolean "or"

A vertical bar separates alternatives. For example,  can match "gray" or "grey".

Grouping

Parentheses are used to define the scope and precedence of the operators (among other uses). For example,  and   are equivalent patterns which both describe the set of "gray" and "grey".

Quantification

A quantifier after a token (such as a character) or group specifies how often that preceding element is allowed to occur.
 * The question mark indicates there is zero or one of the preceding element. For example,  matches both "color" and "colour".
 * The asterisk indicates there is zero or more of the preceding element. For example,  matches "ac", "abc", "abbc", "abbbc", and so on.
 * The plus sign indicates there is one or more of the preceding element. For example,  matches "abc", "abbc", "abbbc", and so on, but not "ac".

Most programming languages have regular expression functions. In VB.NET we can use regular expressions by using the Regex routine: A common use for regular expressions is in checking that you have a correctly typed email address. A rule for that is this:.

You can find out more about Regular expression on wikipedia and you will cover regular expressions in more detail in A2.

to/from integer, real, date/time.