首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 字串列表核心操作大全:TStrings 完整指南

     編輯:Delphi

統一介面 TStrings

Delphi 以 TStrings 統一封裝各類字串列表(ComboBox、ListBox、Memo.Lines、Font 列表、Notebook 頁籤、StringGrid 等),提供一致操作介面,支援跨控制項複製轉換。

常用操作分類

  • 計數:Count(唯讀,索引 0 起算,最大索引 = Count-1)
  • 存取:Strings[Index] 或直接 [Index](Strings 為預設屬性)
  • 搜尋:IndexOf('字串') 回傳位置,找不到為 -1,僅支援完整比對
  • 新增:Add('尾端')、Insert(Index, '中間')
  • 移動:Move(CurrIndex, NewIndex)(連帶物件同步)
  • 刪除:Delete(Index)、Clear(全清)
  • 複製:目標 := 來源(覆寫)、AddStrings(來源)(附加)
  • 批次處理:for i := 0 to List.Count-1 do ...

實用代碼片段

判斷檔案是否存在於列表

if FileListBox1.Items.IndexOf('AUTOEXEC.BAT') > -1 then
begin
  Color := clYellow;
  Label1.Caption := 'You are in the root directory!';
end;

安全刪除指定字串

with ListBox1.Items do
  if IndexOf('bureaucracy') > -1 then
    Delete(IndexOf('bureaucracy'));

列表項目大小寫轉換

for I := 0 to ListBox1.Items.Count - 1 do
  ListBox1.Items[I] := UpperCase(ListBox1.Items[I]);

設計時編輯器

Object Inspector 中雙擊 TStrings 屬性彈出字串列表編輯器,可視化增刪改項目。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved