SysUtils
8 methodsDelphi SysUtils 单元的核心例程集合。
IntToStr(value: Integer): string将整数转换为字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| value | Integer | 待转换的整数 |
Returns
string,整数的字符串表示
Example
delphi
var
s: string;
begin
s := IntToStr(123);
Writeln(s); // '123'
end;StrToInt(s: string): Integer将字符串转换为整数,失败抛出异常。
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | 数字字符串 |
Returns
Integer,解析得到的整数
Example
delphi
var
n: Integer;
begin
n := StrToInt('456');
Writeln(n); // 456
end;Format(fmt: string; args: array of const): string按格式字符串格式化参数列表。
Parameters
| Name | Type | Description |
|---|---|---|
| fmt | string | 格式字符串,如 '%d %s' |
| args | array 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
| Name | Type | Description |
|---|---|---|
| s | string | 待处理的字符串 |
Returns
string,去除空白后的字符串
Example
delphi
var
s: string;
begin
s := Trim(' hello ');
Writeln('|' + s + '|'); // '|hello|'
end;Pos(substr, s: string): Integer返回子串在字符串中首次出现的位置(从 1 开始),未找到返回 0。
Parameters
| Name | Type | Description |
|---|---|---|
| substr | string | 要查找的子串 |
| s | string | 被查找的字符串 |
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
| Name | Type | Description |
|---|---|---|
| s | string | 源字符串 |
| start | Integer | 起始位置(1-based) |
| count | Integer | 截取长度 |
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
| Name | Type | Description |
|---|---|---|
| f | File | 文件变量 |
| name | string | 文件名 |
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
| Name | Type | Description |
|---|---|---|
| s | string | 数字字符串 |
| value | out Integer | 输出的整数 |
Returns
Boolean,转换是否成功
Example
delphi
var
n: Integer;
begin
if TryStrToInt('99', n) then
Writeln(n); // 99
end;