本文為 Delphi 6 SysUtils.pas 函數大全系列第四部分,涵蓋 浮點數/貨幣格式化、字串轉數值、日期時間編碼/解碼/格式化、系統時間/當前年份、月份運算、閏年判斷、日期時間轉字串/字串轉日期時間、系統錯誤訊息、地區設定、格式設定還原、語言資訊、記憶體分配、退出程序、字串大小寫/比較/修剪/引用、字串轉整數/布林、資源字串載入、檔案開關/讀寫/搜尋/屬性/刪除/重命名/路徑拆解/磁碟空間。
核心函數分類速查
1. 數值格式化與轉換
| 函數 | 用途 | 關鍵參數 |
FormatFloat | 浮點數依格式字串轉字串 | FormatFloat(',.00', 1234567890) = '1,234,567,890.00' |
FormatCurr | 貨幣依格式字串轉字串 | 同 FormatFloat |
FloatToTextFmt | 低階格式化至 PChar 緩衝區 | 回傳寫入長度 |
StrToFloat / StrToFloatDef / TryStrToFloat | 字串轉浮點數 | Def/Try 避免例外 |
StrToCurr / StrToCurrDef / TryStrToCurr | 字串轉貨幣 | |
TextToFloat | PChar 緩衝區轉無型別數值 | ValueType 指定目標類型 |
FloatToDecimal | 浮點數轉 TFloatRec 結構 | Precision/Decimals 控制精度 |
2. 日期時間核心操作
| 分類 | 函數 | 用途 |
| 編碼 | EncodeDate | 年月日 → TDateTime |
EncodeTime | 時分秒毫秒 → TDateTime |
TryEncodeDate | 安全編碼日期,失敗回 False |
TryEncodeTime | 安全編碼時間,失敗回 False |
| 解碼 | DecodeDate | TDateTime → 年月日 |
DecodeDateFully | TDateTime → 年月日星期 |
DecodeTime | TDateTime → 時分秒毫秒 |
| 轉換 | DateTimeToTimeStamp | TDateTime ↔ TTimeStamp |
TimeStampToDateTime | |
| 毫秒互轉 | MSecsToTimeStamp | Comp 毫秒 ↔ TTimeStamp |
TimeStampToMSecs | |
| 系統時間 | DateTimeToSystemTime | TDateTime ↔ TSystemTime (Win32 API) |
SystemTimeToDateTime | |
| 星期 | DayOfWeek | 1=週日...7=週六 |
| 當前值 | Date / Time / Now | 僅日期/僅時間/日期時間 |
| 年份 | CurrentYear | 當前年份 Word |
3. 日期時間運算與格式化
| 函數 | 用途 | 範例 |
IncMonth | 增加/減少月份 | IncMonth(Date, -3) 三個月前 |
IncAMonth | 直接操作年月日變數 | 程序而非函數 |
ReplaceTime | 替換日期時間的時間部分 | Int(DT) + Frac(NewTime) |
ReplaceDate | 替換日期時間的日期部分 | Int(NewDate) + Frac(DT) |
IsLeapYear | 判斷閏年 | (Y mod 4=0) and ((Y mod 100<>0) or (Y mod 400=0)) |
DateToStr / TimeToStr / DateTimeToStr | 依系統格式轉字串 | ShortDateFormat/LongTimeFormat |
StrToDate / StrToDateDef / TryStrToDate | 字串轉日期 | |
StrToTime / StrToTimeDef / TryStrToTime | 字串轉時間 | |
StrToDateTime / StrToDateTimeDef / TryStrToDateTime | 字串轉日期時間 | |
FormatDateTime | 自訂格式轉字串 | 'YYYY"年"MM"月"DD"日"' |
DateTimeToString | 程序版 FormatDateTime | 結果存入 var 參數 |
FloatToDateTime | 浮點數 (OADate) 轉 TDateTime | Excel 日期互轉 |
4. 系統與地區資訊
| 函數 | 用途 |
SysErrorMessage | 錯誤碼對應系統訊息字串 |
GetLocaleStr | 取得地區設定字串 (如語言名稱) |
GetLocaleChar | 取得地區設定字元 (如千分位符號) |
GetFormatSettings | 還原系統預設格式設定 |
Languages | 取得系統支援語言集合物件 |
5. 記憶體與退出程序
| 函數/程序 | 用途 |
AllocMem | 配置歸零記憶體區塊 (搭配 FreeMem) |
AddExitProc | 註冊程式結束執行程序 (建議用 finalization) |
6. 字串處理核心 (Ansi/Wide 版)
| 分類 | Ansi 版 | Wide 版 | 用途 |
| 大小寫 | AnsiUpperCase/LowerCase | WideUpperCase/LowerCase | 轉換大小寫 |
| 比較 | AnsiCompareStr/Text/SameStr/Text | WideCompareStr/Text/SameStr/Text | 比較/相等判斷 |
| 修剪 | Trim/TrimLeft/TrimRight | 同名多載 | 移除不可見字元 (≤#31) |
| 引用 | QuotedStr / AnsiQuotedStr | — | Pascal/自訂引用字元 |
| 去引用 | AnsiExtractQuotedStr / AnsiDequotedStr | — | 還原引用字串本體 |
| 行尾 | AdjustLineBreaks | — | 統一為 CRLF/LF |
| 識別符 | IsValidIdent | — | 驗證合法 Pascal 識別符 |
7. 整數/布林轉換
| 函數 | 用途 | 備註 |
IntToStr / IntToHex | 整數 → 字串/十六進制 | IntToHex 指定最小寬度補 0 |
StrToInt / StrToIntDef / TryStrToInt | 字串 → 整數 | Try 版最安全 |
StrToInt64 / Def / Try | 字串 → Int64 | |
StrToBool / Def / Try | 字串 → 布林 | D6 Bug:TryStrToBool 需修正 TrueBoolStrs/FalseBoolStrs |
BoolToStr | 布林 → 字串 | UseBoolStrs=True 時輸出 -1/0 |
8. 資源字串
LoadStr(Ident):載入字串資源
FmtLoadStr(Ident, Args):格式化載入字串資源
9. 檔案/目錄完整操作集
| 分類 | 函數 | 用途/回傳 |
| 低層檔案控制代碼 | FileOpen | 開啟檔案,回傳 Handle (失敗 < 0),Mode: fmOpenRead/Write/ReadWrite |
FileCreate | 建立檔案,回傳 Handle |
FileRead | 讀取至緩衝區,回傳位元組數 |
FileWrite | 寫入緩衝區,回傳位元組數 |
FileSeek | 移動指標,Origin: 0=首, 1=目前, 2=尾 |
FileClose | 關閉 Handle |
| 檔案資訊與屬性 | FileAge | 檔案建立時間 (整數編碼),不存在 -1 |
FileExists | 檔案是否存在 (含隱藏) |
DirectoryExists | 目錄是否存在 |
ForceDirectories | 遞迴建立多層目錄 |
FileGetDate / FileSetDate | 取得/設定檔案修改時間 |
FileGetAttr / FileSetAttr | 取得/設定檔案屬性 |
FileIsReadOnly / FileSetReadOnly | 唯讀檢查/設定 |
| 檔案操作 | DeleteFile | 刪除檔案 |
RenameFile | 重命名 |
ChangeFileExt | 變更副檔名 (注意需加點) |
| 路徑拆解 | ExtractFilePath | 目錄路徑 (含尾斜線) |
ExtractFileDir | 目錄路徑 (不含尾斜線) |
ExtractFileDrive | 磁碟機代號 |
ExtractFileName | 純檔名 |
ExtractFileExt | 副檔名 (含點) |
ExpandFileName | 相對路徑轉完整路徑 |
ExpandFileNameCase | 大小寫敏感擴展路徑 |
ExpandUNCFileName | UNC 路徑展開 |
ExtractRelativePath | 基準路徑 → 目標相對路徑 |
ExtractShortPathName | 8.3 短檔名 |
FileSearch | 目錄列表中搜尋檔案 |
| 磁碟空間 | DiskFree | 可用空間 (Byte, Int64),Drive: 0=目前, 1=A... |
DiskSize | 總空間 |
| 時間編碼互轉 | FileDateToDateTime | 檔案時間整數 → TDateTime |
DateTimeToFileDate | TDateTime → 檔案時間整數 |
| 目錄操作 | GetCurrentDir | 目前工作目錄 |
SetCurrentDir | 設定工作目錄 |
| 目錄建立/刪除 | CreateDir | 建立單層目錄 |
RemoveDir | 刪除空目錄 |
ForceDirectories | 遞迴建立多層目錄 |
| 檔案搜尋 | FindFirst | 開始搜尋,Attr: faAnyFile/faDirectory...,回傳 0 成功 |
FindNext | 繼續搜尋 |
FindClose | 結束搜尋 (必須呼叫釋資源) |
實用範例精選
遞迴建立目錄並檢查磁碟空間
if ForceDirectories('D:\Data\Project\Backup') then
if DiskFree(4) > 100 * 1024 * 1024 then // 100MB
// 執行備份...
安全轉換使用者輸入日期
var DT: TDateTime;
begin
if TryStrToDateTime(Edit1.Text, DT) then
DateTimePicker1.DateTime := DT
else
ShowMessage('日期格式錯誤');
end;
列舉目錄所有檔案
var SR: TSearchRec;
begin
if FindFirst('C:\Temp\*.*', faAnyFile, SR) = 0 then try
repeat
Memo1.Lines.Add(SR.Name);
until FindNext(SR) <> 0;
finally FindClose(SR); end;
本部分涵蓋的函數構成 Delphi 應用程式 80% 以上的系統互動基礎,建議開發者熟記高頻函數簽名,並善用 Try* 系列避免例外干擾主流程。