How to get the last string in TSTRINGLIST

The days I have been looking for how to do this, there is nothing I need to do (or I just don’t understand how to implement a solution).

< p>What I need to do is to parse a string (a street address) into a TStringList, then set these strings from the list as variables and then pass them to the rest of the program. I have the list running normally:

var
AddressList: TStringList;
i: integer;

begin
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 length is different. Address Sometimes it will be 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 as variables for later use. I would use TStringList.Find, but the zip codes are not always the same. Is there a way to get the last string and then back from there? (Go back, because once I get City, the status ZIP I can delete it from RawAddressStr and set the rest as an address string.)

Thank you.

Edit, this Is the code I need (thanks for the comments below):
AddressList:= TStringList.Create;

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

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

Now I can use StringReplace to extract City, State Zip from the complete address string , And set it to the address string to be used.

Because you asked, I’m not Determine what you are looking for
How to get the last string of StringList,
Then at the end of the question you asked

Is there a way to get the last string and then go backwards from
there?

If you want to get the last string of StringList, you can use

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

If you want to reverse or reverse loop, you should write:

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

< p>Please note that it means “DOWNTO” instead of “TO”.

Now, if you want to stop at a specific string, let’s say the zip code,
you need to make your The software understands what it is reading.

Which delimited string is the city?
Which one is the address?
For software, a string is a string. It doesn’t know or even care what it is reading.

So I would suggest you have a city database that can compare strings< br>Address list t, o, and see if they match.

You can also implement some logic in the algorithm.
If you know that the last string of the separated AdressList string is always City- name,
Then you know that you have a city name, you can use it.

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

The days I have been looking for how to do this, there is nothing I need to do (Or I just don’t understand how to implement the solution).

What I need to do is to parse a string (a street address) into a TStringList, and then set these strings from the list to The variables are then passed to the rest of the program. I have run the list normally:

var
AddressList: TStringList;
i: integer;

begin
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 length is different. Sometimes the address will be 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 as variables for For future use. I will use TStringList.Find, but the zip codes are not always the same. Is there a way to get the last string and then back from there? (Go back, because once I get City, the status ZIP I can delete it from RawAddressStr and set the rest as an address string.)

Thank you.

Edit, this Is the code I need (thanks for the comments below):
AddressList:= TStringList.Create;

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

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

Now I can use StringReplace to extract City, State Zip from the complete address string , And set it to the address string to be used.

Because you asked, I am a bit unsure what you are looking for
how to get the StringList The last string,
then at the end of the question you asked

Is there a way to get the last string and then go backwards from
there?

If you want to get the last string of StringList, you can use

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

If you want to reverse or reverse the loop, you should write:

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

Please note that it means “DOWNTO” instead of “TO”.

Now, if you want to stop at a specific string, let’s say the zip code,
you need to make your software understand what it is reading.

Which delimiting character The string is the city?
Which one is the address?
For software, a string is a string. It doesn’t know or even care what it is reading.

So I would suggest you have a city database that can compare strings< br>Address list t, o, and see if they match.

You can also implement some logic in the algorithm.
If you know that the last string of the separated AdressList string is always City- name,
Then you know that you have a city name, you can use it.

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

Leave a Comment

Your email address will not be published.