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

Delphi 6 字串處理函數完整參考:StrUtils.pas 核心功能詳解

     編輯:Delphi

Delphi 6 StrUtils.pas 字串函數速查指南

StrUtils.pas 是 Delphi 6 引入的強大字串處理單元,提供了大量不區分大小寫與區分大小寫的字串比較、搜尋、替換、擷取及轉換函數。本文系統整理該單元核心函數的語法、功能、參數說明與實戰範例,供開發者快速查閱。

不區分大小寫的字串比較與搜尋

AnsiResemblesText - 字串相似度比較

function AnsiResemblesText(const AText, AOther: string): Boolean;

功能:判斷兩個字串是否相似(基於 Soundex 演算法)。
說明:不區分大小寫,適用於模糊匹配場景。
參考:SoundexProc、AnsiResemblesProc
範例:CheckBox1.Checked := AnsiResemblesText(Edit1.Text, Edit2.Text);

AnsiContainsText - 子字串包含檢查

function AnsiContainsText(const AText, ASubText: string): Boolean;

功能:檢查 AText 是否包含 ASubText。
說明:不區分大小寫。
參考:AnsiUppercase、AnsiPos
範例:CheckBox1.Checked := AnsiContainsText(Edit1.Text, Edit2.Text);

AnsiStartsText / AnsiEndsText - 前綴/後綴判斷

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);

AnsiReplaceText - 不區分大小寫替換

function AnsiReplaceText(const AText, AFromText, AToText: string): string;

功能:將 AText 中所有 AFromText 替換為 AToText。
說明:不區分大小寫。
參考:SysUtils.StringReplace、SysUtils.TReplaceFlags
範例:Edit4.Text := AnsiReplaceText(Edit1.Text, Edit2.Text, Edit3.Text);

AnsiMatchText / AnsiIndexText - 陣列查找

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。用法相同,唯一差異在於嚴格區分字母大小寫。

字串構建與變換工具函數

DupeString - 字串重複

function DupeString(const AText: string; ACount: Integer): string;

功能:返回 AText 重複 ACount 次的字串。
說明:ACount = 0 時返回空字串。
範例:Edit3.Text := DupeString(Edit1.Text, SpinEdit1.Value);

ReverseString - 字串反轉

function ReverseString(const AText: string): string;

功能:返回字串的反序排列。
範例:ReverseString('1234') = '4321'

StuffString - 字串插入/取代

function StuffString(const AText: string; AStart, ALength: Cardinal; const ASubText: string): string;

功能:在 AText 的 AStart 位置,刪除 ALength 個字元並插入 ASubText。
範例:StuffString('abcd', 2, 0, '12') = 'a12bcd'

RandomFrom - 隨機選取陣列元素

function RandomFrom(const AValues: array of string): string;

功能:從字串陣列中隨機返回一個元素。
說明:建議先呼叫 Randomize。
範例:Randomize; Edit3.Text := RandomFrom(['a1', 'a2', 'a3', 'a4']);

IfThen - 條件字串返回

function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string;

功能:根據布林值返回對應字串。
範例:IfThen(True, '是', '否') = '是'

LeftStr / RightStr / MidStr - 字串擷取

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'

進階搜尋與 Soundex 語音編碼

SearchBuf - 緩衝區字串搜尋

function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer;
  SearchString: String; Options: TStringSearchOptions = [soDown]): PChar;

功能:在記憶體緩衝區中搜尋字串,返回首次匹配位置指標。
應用:常用於編輯控制元件的尋找功能實作。
範例:見原始碼中的 SearchEdit 完整實作。

Soundex 系列函數 - 語音相似度編碼

基於 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);

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