A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2011 Exam/String manipulation
Visual Basic allows you to do many things with strings. This is particularly important if you are wantint to extract data from text, search text, or generally manipulate text. Some of the functions you might be interested in are:
- ToUpper 'converts the entire string into Upper case
- ToLower 'converts the entire string into Lower case
- Split 'splits a string into different parts, see below
For the exam you are dealing with a CSV file. This has comma separated values, so we might have to find the data that is hiding in the commas. Take a look at the example below.
Sub Main()
Dim temp As String
temp = "Henry,45,Michael,34,Sara,67,Barry,90"
Dim fields() As String
'load the fields array with all the items
fields = Split(temp, ",") ' split the temp string into array elements depending on where it finds the comma
' loop through each of the items in the array fields and print them on separate lines
For i = 0 To UBound(fields) 'UBound gets the number of items in the array fields, in this case 7 (0-7, 8 items)
Console.WriteLine(Trim$(fields(i))) ' Trim takes off all the extra white spaces
Next
Console.ReadLine()
End Sub
Output:
Henry 45 Michael 34 Sara 67 Barry 90