StrUtils.pas 是 Delphi 6 引入的強大字串處理單元,提供了大量不區分大小寫與區分大小寫的字串比較、搜尋、替換、擷取及轉換函數。本文系統整理該單元核心函數的語法、功能、參數說明與實戰範例,供開發者快速查閱。
function AnsiResemblesText(const AText, AOther: string): Boolean;
功能:判斷兩個字串是否相似(基於 Soundex 演算法)。
說明:不區分大小寫,適用於模糊匹配場景。
參考:SoundexProc、AnsiResemblesProc
範例:CheckBox1.Checked := AnsiResemblesText(Edit1.Text, Edit2.Text);
function AnsiContainsText(const AText, ASubText: string): Boolean;
功能:檢查 AText 是否包含 ASubText。
說明:不區分大小寫。
參考:AnsiUppercase、AnsiPos
範例:CheckBox1.Checked := AnsiContainsText(Edit1.Text, Edit2.Text);
function AnsiStartsText(const ASubText, AText: string): Boolean;
function AnsiEndsText(const ASubText, AText: string): Boolean;
功能:判斷 AText 是否以 ASubText 開頭或結尾。
說明:不區分大小寫。
參考:Windows.CompareString
範例:CheckBox1.Checked := AnsiStartsText(Edit1.Text, Edit2.Text);
function AnsiReplaceText(const AText, AFromText, AToText: string): string;
功能:將 AText 中所有 AFromText 替換為 AToText。
說明:不區分大小寫。
參考:SysUtils.StringReplace、SysUtils.TReplaceFlags
範例:Edit4.Text := AnsiReplaceText(Edit1.Text, Edit2.Text, Edit3.Text);
function AnsiMatchText(const AText: string; const AValues: array of string): Boolean;
function AnsiIndexText(const AText: string; const AValues: array of string): Integer;
功能:檢查字串陣列 AValues 是否包含 AText,或返回其索引(未找到返回 -1)。
說明:不區分大小寫。
參考:AnsiIndexText、SysUtils.AnsiSameText
範例:CheckBox1.Checked := AnsiMatchText(Edit1.Text, ['a1', 'a2', 'a3', 'a4']);
對應上述函數的區分大小寫版本:AnsiContainsStr、AnsiStartsStr、AnsiEndsStr、AnsiReplaceStr、AnsiMatchStr、AnsiIndexStr。用法相同,唯一差異在於嚴格區分字母大小寫。
function DupeString(const AText: string; ACount: Integer): string;
功能:返回 AText 重複 ACount 次的字串。
說明:ACount = 0 時返回空字串。
範例:Edit3.Text := DupeString(Edit1.Text, SpinEdit1.Value);
function ReverseString(const AText: string): string;
功能:返回字串的反序排列。
範例:ReverseString('1234') = '4321'
function StuffString(const AText: string; AStart, ALength: Cardinal; const ASubText: string): string;
功能:在 AText 的 AStart 位置,刪除 ALength 個字元並插入 ASubText。
範例:StuffString('abcd', 2, 0, '12') = 'a12bcd'
function RandomFrom(const AValues: array of string): string;
功能:從字串陣列中隨機返回一個元素。
說明:建議先呼叫 Randomize。
範例:Randomize; Edit3.Text := RandomFrom(['a1', 'a2', 'a3', 'a4']);
function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string;
功能:根據布林值返回對應字串。
範例:IfThen(True, '是', '否') = '是'
function LeftStr(const AText: string; const ACount: Integer): string;
function RightStr(const AText: string; const ACount: Integer): string;
function MidStr(const AText: string; const AStart, ACount: Integer): string;
功能:分別擷取左側、右側、指定位置的子字串。
說明:本質上封裝了 System.Copy。
範例:LeftStr('123456', 3) = '123'
function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer;
SearchString: String; Options: TStringSearchOptions = [soDown]): PChar;
功能:在記憶體緩衝區中搜尋字串,返回首次匹配位置指標。
應用:常用於編輯控制元件的尋找功能實作。
範例:見原始碼中的 SearchEdit 完整實作。
基於 Soundex 演算法(美國國家檔案館標準),將字串轉換為語音編碼,用於模糊匹配姓名等場景。
Soundex(const AText: string; ALength: TSoundexLength = 4): string - 返回編碼字串SoundexInt(...): Integer - 返回編碼整數,長度越大精度越高DecodeSoundexInt(AValue: Integer): string - 整數編碼解碼SoundexWord(...): Word - 固定長度 4 的編碼DecodeSoundexWord(AValue: Word): string - Word 編碼解碼SoundexSimilar(...): Boolean - 判斷兩字串編碼是否相同SoundexCompare(...): Integer - 比較兩字串編碼大小SoundexProc - 系統變數 AnsiResemblesProc 的預設實作範例:Edit2.Text := Soundex(Edit1.Text, SpinEdit1.Value);