{"id":299,"date":"2009-04-06T13:44:00","date_gmt":"2009-04-06T05:44:00","guid":{"rendered":""},"modified":"2013-11-17T19:42:25","modified_gmt":"2013-11-17T11:42:25","slug":"tstringlist-%e5%b8%b8%e7%94%a8%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/299.html","title":{"rendered":"TStringList \u5e38\u7528\u64cd\u4f5c"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/TStringList \u5e38\u7528\u65b9\u6cd5\u4e0e\u5c5e\u6027:\r\nvar\r\nList: TStringList;\r\ni: Integer;\r\nbegin\r\nList := TStringList.Create;\r\nList.Add('Strings1');           {\u6dfb\u52a0}\r\nList.Add('Strings2');\r\nList.Exchange(0,1);             {\u7f6e\u6362}\r\nList.Insert(0,'Strings3');      {\u63d2\u5165}\r\ni := List.IndexOf('Strings1'); {\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e}\r\nList.Sort;                      {\u6392\u5e8f}\r\nList.Sorted := True; {\u6307\u5b9a\u6392\u5e8f}\r\nList.Count;                     {\u603b\u6570}\r\nList.Text;                      {\u6587\u672c\u96c6\u5408}\r\nList.Delete(0);                 {\u5220\u9664, 0\u662f\u7b2c\u4e00\u4e2a\u6570\u636e}\r\nList.LoadFromFile('c:\\tmp.txt');{\u6253\u5f00}\r\nList.SaveToFile('c:\\tmp.txt'); {\u4fdd\u5b58}\r\nList.Clear;                     {\u6e05\u7a7a}\r\nList.Free;                      {\u91ca\u653e}\r\nend;\r\n\r\n\/\/\u8bfb\u5165\u5b57\u7b26\u4e32\r\nvar\r\nList: TStringList;\r\nbegin\r\nList := TStringList.Create;\r\nList.CommaText := 'aaa,bbb,ccc,ddd';\r\n\/\/\u76f8\u5f53\u4e8e: List.Text := 'aaa' + #13#10 + 'bbb' + #13#10' + 'ccc' + '#13#10' + 'ddd';\r\n\r\nShowMessage(IntToStr(List.Count)); \/\/4\r\nShowMessage(List&#x5B;0]); \/\/aaa\r\n\r\nList.Free;\r\nend;\r\n\r\n\/\/\u7f6e\u6362\u5206\u9694\u7b26\r\nvar\r\nList: TStringList;\r\nbegin\r\nList := TStringList.Create;\r\nList.Delimiter := '|';\r\nList.DelimitedText := 'aaa|bbb|ccc|ddd';\r\n\r\nShowMessage(IntToStr(List.Count)); \/\/4\r\nShowMessage(List&#x5B;0]); \/\/aaa\r\n\r\nList.Free;\r\nend;\r\n\r\n\/\/\u7c7b\u4f3c\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u6cd5\r\nvar\r\nList: TStringList;\r\nbegin\r\nList := TStringList.Create;\r\n\r\nList.Add('aaa=111');\r\nList.Add('bbb=222');\r\nList.Add('ccc=333');\r\nList.Add('ddd=444');\r\n\r\nShowMessage(List.Names&#x5B;1]); \/\/bbb\r\nShowMessage(List.ValueFromIndex&#x5B;1]); \/\/222\r\nShowMessage(List.Values&#x5B;'bbb']); \/\/222\r\n\r\n\/\/ValueFromIndex \u53ef\u4ee5\u8d4b\u503c:\r\nList.ValueFromIndex&#x5B;1] := '2';\r\nShowMessage(List&#x5B;1]); \/\/bbb=2\r\n\r\n\/\/\u53ef\u4ee5\u901a\u8fc7 Values \u8d4b\u503c:\r\nList.Values&#x5B;'bbb'] := '22';\r\nShowMessage(List&#x5B;1]); \/\/bbb=22\r\n\r\nList.Free;\r\nend;\r\n\r\n\/\/\u907f\u514d\u91cd\u590d\u503c\r\nvar\r\nList: TStringList;\r\nbegin\r\nList := TStringList.Create;\r\n\r\nList.Add('aaa');\r\n\r\nList.Sorted := True; \/\/\u9700\u8981\u5148\u6307\u5b9a\u6392\u5e8f\r\nList.Duplicates := dupIgnore; \/\/\u5982\u6709\u91cd\u590d\u503c\u5219\u653e\u5f03\r\n\r\nList.Add('aaa');\r\n\r\nShowMessage(List.Text); \/\/aaa\r\n\r\n\/\/Duplicates \u67093\u4e2a\u53ef\u9009\u503c:\r\n\/\/dupIgnore: \u653e\u5f03;\r\n\/\/dupAccept: \u7ed3\u675f;\r\n\/\/dupError: \u63d0\u793a\u9519\u8bef.\r\n\r\nList.Free;\r\nend;\r\n\r\n\/\/\u6392\u5e8f\u4e0e\u5012\u6392\u5e8f\r\n{\u6392\u5e8f\u51fd\u6570}\r\nfunction DescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;\r\nbegin\r\nResult := -AnsiCompareText(List&#x5B;Index1], List&#x5B;Index2]);\r\nend;\r\n\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nvar\r\nList: TStringList;\r\nbegin\r\nList := TStringList.Create;\r\n\r\nList.Add('bbb');\r\nList.Add('ccc');\r\nList.Add('aaa');\r\n\r\n\/\/\u672a\u6392\u5e8f\r\nShowMessage(List.Text); \/\/bbb ccc aaa\r\n\r\n\/\/\u6392\u5e8f\r\nList.Sort;\r\nShowMessage(List.Text); \/\/aaa bbb ccc\r\n\r\n\/\/\u5012\u6392\u5e8f\r\nList.CustomSort(DescCompareStrings); \/\/\u8c03\u7528\u6392\u5e8f\u51fd\u6570\r\nShowMessage(List.Text); \/\/ccc bbb aaa\r\n\r\n\/\/\u5047\u5982:\r\nList.Sorted := True;\r\nList.Add('999');\r\nList.Add('000');\r\nList.Add('zzz');\r\nShowMessage(List.Text); \/\/000 999 aaa bbb ccc zzz\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/TStringList \u5e38\u7528\u65b9\u6cd5\u4e0e\u5c5e\u6027: var List: TStringList; i: Intege [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":4776,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions\/4776"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}