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.
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
Len("some string") function that returns the integer length of the string that it has been passed:
someText = "Gary had a little lamb" Console.writeline(Len(someText))
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:
someText = "Gary had a little lamb. His fleece was white as snow." Console.writeline(InStr(someText,"."))
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:
someText = "Gary had a little lamb. Dave's fleece was white as snow." Console.writeline(InStr(someText,"Dave"))
If the search item is not contained in the string then it will return 0
someText = "Gary had a little lamb. Dave's fleece was white as snow." Console.writeline(InStr(someText,"Julie"))
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:
phone = "(01234)567890" local = phone.Substring(7, 6) console.writeline(local)
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:
[stringA & stringB]
For example we might have a users name stored in a variable
dim name as string and a greeting that we would like to give them:
name = "Charles" console.writeline("Hello " & name & ". 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:
dim age as integer
- would allow:
age = 34
- would NOT allow:
age = "cabbages"
This seems to make sense, but what would happen when you try to place a real number into a integer:
dim age as integer age = 34.3 console.writeline(age)
This might seem OK, but in other lanuages we might run into trouble. To perform this we would have to convert from one datatype to another:
dim age as decimal age = 34.3 console.writeline(age) age = CInt(34.3) 'converts the decimal into an integer console.writeline(age)
Dim name As String console.write("Input: ") name = console.readline() console.writeline("Hello " & name & " you have " & Len(name) & " letters in your name.")
Dim name As String = "Elizabeth Sheerin" Dim firstname, secondname As String Dim space, textlength As Integer space = InStr(name, " ") textlength = Len(name) firstname = name.Substring(0, space) secondname = name.Substring(space, textlength - space) Console.WriteLine("first name is: " & firstname) Console.WriteLine("second name is: " & secondname)
Dim phonenum As String = "(01234)567890" Dim firstbracket, secondbracket As String Dim textlength, arealength As Integer firstbracket = InStr(phonenum, "(") secondbracket = InStr(phonenum, ")") textlength = Len(phonenum) arealength = secondbracket - firstbracket Console.Write(phonenum.Substring(firstbracket, arealength - 1) & phonenum.Substring(secondbracket, textlength - secondbracket))
Dim phonenum As String = "01234567890" Console.Write("(" & phonenum.Substring(0, 5) & ")" & phonenum.Substring(6, 5)) Console.ReadLine()
Dim name As String Dim length As Integer Dim Pal As Boolean = TRUE console.write("Input: ") name = console.readline() length = Len(name) For x = 0 to (length / 2) If name.Substring(x, 1) != name.Substring(length - x, 1) then Pal = FALSE End If Next If Pal then console.writeline("That is a palindrome!") Else console.writeline("That is NOT a palindrome!") End If
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:
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
Most regular expression tools provide the following operations to construct expressions.
A vertical bar separates alternatives. For example,
Parentheses are used to define the scope and precedence of the operators (among other uses). For example,
A quantifier after a token (such as a character) or group specifies how often that preceding element is allowed to occur.
Most programming languages have regular expression functions. In VB.NET we can use regular expressions by using the Regex routine:
' this code enforces the name rule from earlier Dim name As String Console.Write("Name of best friend: ") name = Console.Readline() ' match the string against a regular expression Dim m As Match = Regex.Match(name, "[A-Z][a-z]+") If (m.Success) Then Console.WriteLine("You have input the name correctly") Else Console.WriteLine("Incorrect format!") End If
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.