如何将一行文本(例如,Hello there*3)拆分为一个数组?之前的所有内容都*需要添加到第一个元素,之后的所有内容都*需要添加到第二个元素。我确定这是可能的。稍后我需要回忆一下,第一项和第二项必须相互关联
我正在使用德尔福 7。
type
TStringPair = array[0..1] of string;
function SplitStrAtAmpersand(const Str: string): TStringPair;
var
p: integer;
begin
p := Pos('&', Str);
if p = 0 then
p := MaxInt - 1;
result[0] := Copy(Str, 1, p - 1);
result[1] := Copy(Str, p + 1);
end;
Run Code Online (Sandbox Code Playgroud)
或者,如果你不喜欢魔法,
function SplitStrAtAmpersand(const Str: string): TStringPair;
var
p: integer;
begin
p := Pos('&', Str);
if p > 0 then
begin
result[0] := Copy(Str, 1, p - 1);
result[1] := Copy(Str, p + 1);
end
else
begin
result[0] := Str;
result[1] := '';
end;
end;
Run Code Online (Sandbox Code Playgroud)
如果您出于某种非常奇怪和稍微奇怪的原因需要一个过程而不是一个函数,那么请执行以下操作
procedure SplitStrAtAmpersand(const Str: string; out StringPair: TStringPair);
var
p: integer;
begin
p := Pos('&', Str);
if p > 0 then
begin
StringPair[0] := Copy(Str, 1, p - 1);
StringPair[1] := Copy(Str, p + 1);
end
else
begin
StringPair[0] := Str;
StringPair[1] := '';
end;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
711 次 |
| 最近记录: |