Skip to content

Delphi RTL API

Delphi RTL SysUtils unit providing string, formatting, file and type conversion base functions.

1 class · 8 methods

SysUtils

8 methods

Delphi SysUtils 单元的核心例程集合。

IntToStr(value: Integer): string

将整数转换为字符串。

Parameters

NameTypeDescription
valueInteger待转换的整数

Returns

string,整数的字符串表示

Example

delphi
var
  s: string;
begin
  s := IntToStr(123);
  Writeln(s);  // '123'
end;
StrToInt(s: string): Integer

将字符串转换为整数,失败抛出异常。

Parameters

NameTypeDescription
sstring数字字符串

Returns

Integer,解析得到的整数

Example

delphi
var
  n: Integer;
begin
  n := StrToInt('456');
  Writeln(n);  // 456
end;
Format(fmt: string; args: array of const): string

按格式字符串格式化参数列表。

Parameters

NameTypeDescription
fmtstring格式字符串,如 '%d %s'
argsarray of const参数数组

Returns

string,格式化后的字符串

Example

delphi
var
  s: string;
begin
  s := Format('%s is %d', ['answer', 42]);
  Writeln(s);  // 'answer is 42'
end;
Trim(s: string): string

去除字符串首尾空白字符。

Parameters

NameTypeDescription
sstring待处理的字符串

Returns

string,去除空白后的字符串

Example

delphi
var
  s: string;
begin
  s := Trim('  hello  ');
  Writeln('|' + s + '|');  // '|hello|'
end;
Pos(substr, s: string): Integer

返回子串在字符串中首次出现的位置(从 1 开始),未找到返回 0。

Parameters

NameTypeDescription
substrstring要查找的子串
sstring被查找的字符串

Returns

Integer,子串起始位置(1-based),0 表示未找到

Example

delphi
var
  p: Integer;
begin
  p := Pos('lo', 'hello');
  Writeln(p);  // 4
end;
Copy(s: string; start, count: Integer): string

从字符串指定位置(1-based)截取指定长度的子串。

Parameters

NameTypeDescription
sstring源字符串
startInteger起始位置(1-based)
countInteger截取长度

Returns

string,截取的子串

Example

delphi
var
  s: string;
begin
  s := Copy('hello world', 7, 5);
  Writeln(s);  // 'world'
end;
AssignFile(var f: File; name: string)

将文件变量与文件名关联,准备后续 IO 操作。

Parameters

NameTypeDescription
fFile文件变量
namestring文件名

Returns

void,无返回值,建立关联

Example

delphi
var
  f: TextFile;
begin
  AssignFile(f, 'data.txt');
  Rewrite(f);
  Writeln(f, 'line');
  CloseFile(f);
end;
TryStrToInt(s: string; out value: Integer): Boolean

尝试将字符串转为整数,成功返回 True 并通过 out 参数返回结果。

Parameters

NameTypeDescription
sstring数字字符串
valueout Integer输出的整数

Returns

Boolean,转换是否成功

Example

delphi
var
  n: Integer;
begin
  if TryStrToInt('99', n) then
    Writeln(n);  // 99
end;