简体   繁体   English

如何在TStringList中获取最后一个字符串

[英]How to get last string in TStringList

I've been searching for days on how to do this, and nothing is exactly what I need to do (or I just don't understand how to implement the solution). 我一直在寻找如何做到这一点的日子,没有什么是我需要做的(或者我只是不明白如何实现解决方案)。

What I need to do is parse a string, which is a street address, into a TStringList, and then set those strings from the list to variables I can then pass to the rest of the program. 我需要做的是将一个字符串(一个街道地址)解析成TStringList,然后将这些字符串从列表中设置为变量然后传递给程序的其余部分。 I already have the list working ok: 我已经把列表运行正常了:

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 issue is that the address isn't always the same length. 问题是地址长度不一样。 The address will sometimes be like '1234 Dark Souls Two Lane Wyoming...' or '1234 Dark Lane Wyoming...' 地址有时候会像'1234 Dark Souls Two Lane Wyoming ......'或'1234 Dark Lane Wyoming ......'

So I need to be able to use this TStringList to set the Zip, State and City into variables for later use. 所以我需要能够使用这个TStringList将Zip,State和City设置为变量供以后使用。 I would use TStringList.Find, but the ZIP code isn't always the same. 我会使用TStringList.Find,但邮政编码并不总是相同。 Is there a way to get the last string and then go backwards from there? 有没有办法获得最后一个字符串,然后从那里后退? (Going backwards because once I get the City, State ZIP I can remove that from the RawAddressStr and then set the rest to the address string.) (向后退,因为一旦我获得City,状态ZIP我可以从RawAddressStr中删除它,然后将其余部分设置为地址字符串。)

Thanks. 谢谢。

Edit, here's the code I needed (thanks to below comments): AddressList := TStringList.Create; 编辑,这是我需要的代码(感谢下面的评论):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 these with StringReplace to take out the City, State Zip from the full address string, and set that as the Address string to use. 现在我可以使用StringReplace从完整地址字符串中取出City,State Zip,并将其设置为要使用的地址字符串。

I am a little bit unsure of exactly what you're looking for since you ask of how to get the last string of your StringList, then at the end of the question you ask 我有点不确定你正在寻找什么,因为你问如何获取你的StringList的最后一个字符串,然后在你问的问题的最后

Is there a way to get the last string and then go backwards from there? 有没有办法获得最后一个字符串,然后从那里后退?

If you want to get the last string of a StringList you can use 如果要获取StringList的最后一个字符串,可以使用

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

if you would like to for-loop in reverse or backwards you should write: 如果你想反向或反向循环,你应该写:

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

Notice that it says "DOWNTO" and not "TO". 请注意,它表示“DOWNTO”而不是“TO”。

Now, if you would like to stop at a specific string, lets say the ZIP code, you need to make your software understand what it is reading. 现在,如果你想停在一个特定的字符串,让我们说邮政编码,你需要让你的软件了解它正在读什么。

Which one of the delimited strings is the City? 哪一个分隔的字符串是城市? Which one is the address? 哪一个是地址? To the software, a string is a string, it doesn't know, or even care what it is reading. 对于软件来说,字符串是一个字符串,它不知道,甚至不关心它正在读什么。

So I would like to suggest that you have a database of citys which it can compare the strings of AddressList t,o and see if there is a match. 所以我想建议你有一个城市数据库,它可以比较AddressList t,o的字符串,看看是否有匹配。

You could also implement some logic in to your algorithm. 您还可以在算法中实现一些逻辑。 If you know that the last string of your delimited AdressList string always is the City-name, then you know you have the city name right there which you can use. 如果您知道分隔的AdressList字符串的最后一个字符串始终是City-name,那么您知道您可以使用城市名称。

If you know that everything between the ZIP code and the City Name is the Street Address, then just copy everything between the ZIP and the City-name and use that as a Street-name information. 如果您知道邮政编码和城市名称之间的所有内容都是街道地址,那么只需复制ZIP和城市名称之间的所有内容,并将其用作街道名称信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM