• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking
computinglesson.com

Working with Strings

There are two key Delphi functions that can be used in combination to do pretty much anything with strings.  In the example below you are asked to read a text file on the Student Share K: Drive.  The file is called 'CSVTemps.CSV'.  

It was produced from Excel and is in Comma Separated Values format, where each item of data is separated by a comma.

The task is to read the temperature value for each month and calculate the average for the year.

To do this you'll need to read each line, find the position of the comma and read from the comma to the end of the line.
CSV file
Picture
Excel Original
Picture

The two useful Delphi functions:


pos(needle,haystack)

The Pos function finds the position of one string needle within another, haystack.  
It returns an integer, which is the first location found.
If the string is not found, 0 is returned. 
The search IS case sensitive.
var
  inputstring : String;
  resultint : integer;
begin
  inputstring := 'The quick brown fox';
  resultint := pos('o',inputstring);  //returns 13
end.

copy(sourcestring,start,end)

The copy function returns a string which is a slice of the source string, starting at positon 'start' and ending at either position 'end' or the actual end of the string, whichever comes first.
var
  inputstring, resultstring : String;
begin
  inputstring := 'The quick brown fox';
  resultstring := copy(inputstring,5,5); 

     // returns 'quick'
end.

You can use these two functions in combination to progressively work through the string, finding characters and slicing the string.  This is essential if you want to find the second or third match in a string.  Pos will only return the first match, so the string must be cut up to find other matches.

Reading the CSV file

A CSV file might look a bit odd, but it's still a text file, just one with a particular structure.

Use the standard Delphi text file functions to open and read the file:

Assign(file variable, full path of file)
Reset(file variable)
Readln(file variable, input string)
Close(file variable)
var
  inputstring,resultstring : String;
  i,resultint : integer;
  inputfile : TextFile;
begin
  Assign(inputfile,'K:\computing\delphi\csvtemps.csv');
  reset(inputfile);
  while not EOF(inputfile) do
  begin
    readln(inputfile,inputstring);
    writeln(inputstring);
  end;
  close(inputfile);

Finding the position of the comma and slicing the string

Modify the while loop to search for the comma with 'pos' and use the resulting position to extract just that part of the input string.


begin
    readln(inputfile,inputstring);
    resultint := pos(',',inputstring);
    resultstring := copy(inputstring,resultint+1,inputstring.Length);
    writeln(resultstring);
end;
Complete the program by making sure it skips the first result (which is the title), convert the result string to an integer, add them all up and average the result.
Powered by Create your own unique website with customizable templates.