How to get the last line in a TStringList

I've been looking for days on how to do this and nothing is what I need to do (or I just don't understand how to implement the solution).

What I need to do is parse the string that is the street address into a TStringList, and then set those strings from the list to variables that I can pass to the rest of the program. I already have a list that works fine:

AddressList : TStringList;
i : integer;

AddressList := TStringList.Create;
AddressList.Delimiter := ' ';
AddressList.DelimitedText := RawAddressStr; //RawAddressStr is parsed from another file and the string result is something like '1234 Dark Souls Lane Wyoming, MI 48419'

for i := 0 to AddressList.Count-1 do
   AddressList.Strings[i]; //Not sure what to do here


The problem is that the address is not always the same length. The address will sometimes look like "1234 Dark Souls Two Lane Wyoming ..." or "1234 Dark Lane Wyoming ..."

So I need to be able to use this TStringList to set Zip, State and City to variables for later use. I would use TStringList.Find, but the ZIP code is not always the same. Is there a way to get the last line and then go backwards? (Going back, because once I get City, State ZIP, I can remove that from RawAddressStr and then set the rest to the address bar.)


Edit, here is the code I need (thanks to below comments): AddressList: = TStringList.Create;

AddressList.Delimiter := ' ';
AddressList.DelimitedText := RawAddressStr;

for i := 0 to AddressList.Count-1 do
  LastIndex := AddressList.Count - 1;
  ZipStr := AddressList[LastIndex];
  StateStr := AddressList[LastIndex - 1];
  CityStr := AddressList[LastIndex - 2];


Now I can use them with StringReplace to pull City, State Zip from full address bar and set that as address bar to use.


source to share

1 answer

I'm a bit unsure what exactly you are looking for, since you are asking how to get the last line of your StringList then at the end of the question you are asking

Is there a way to get the last line and then go back there?

If you want to get the last line of the StringList you can use

  var AddressList : TStringList;
      MyString: string;
  MyString := AddressList.Last; //this...
  MyString := AddressList.Strings[AddressList.Count-1]; //...and this is essentially the same thing


if you want to use a for-loop backward or backward, you should write:

for i := AddressList.Count-1 downto 0 do
  AddressList.Strings[i]; //Not sure what to do here


Note that it says "DOWNTO" and not "TO".

Now if you want to stop at a specific line, say a zip code, you need to make your software understand what it reads.

Which of the split lines is the city? What is the address? To software, a string is a string, it doesn't know or even care what it reads.

So, I would like to suggest you have a city database where it can compare strings from AddressList t, o and see if there is a match.

You can also implement some logic in your algorithm. If you know that the last line of the AdressList delimited string is always the City name, then you know that you have a city name that you can use.

If you know that everything between the zip code and the city name is a Street address, then just copy everything between the ZIP and City-name and use that as the street name information.



All Articles