gpt4 book ai didi

Delphi - 按换行符分割字符串

转载 作者:行者123 更新时间:2023-12-03 15:43:38 39 4
gpt4 key购买 nike

注意:我使用的是 Delphi 7。

有没有办法用换行符分割字符串?

我想要类似以下的内容:

procedure Split
(const Delimiter: Char;
Input: string;
const Strings: TStrings) ;
begin
Assert(Assigned(Strings)) ;
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.DelimitedText := Input;
end;

var
xMSG:String;
xStr:String;
xStrList:TStrings;
xLineBreak:String;
xHello:String;
xWorld:String;
begin
xLineBreak := AnsiString(#13#10);
xMSG := 'Hello ' + xLineBreak + 'World';
xStrList := TStringList.Create;
Split(xLineBreak,AnsiString(xMSG),xStrList);
xHello := xStrList[0];
xWorld := xStrList[1];
MessageBox(0,PAnsiChar(xHello + xWorld),'Test',0);
end.

最佳答案

是的,这就是 Text 属性的作用。引用自帮助(文本属性(TStrings)):

Lists the strings in the TStrings object as a single string with the individual strings delimited by carriage returns and line feeds.

由于它是单行,因此您不需要额外的实用程序。

var
xMSG:String;
// xStr:String;
xStrList:TStrings;
// xLineBreak:String;
xHello:String;
xWorld:String;
begin
// xLineBreak := AnsiString(#13#10); // you don't need this, there's sLineBreak
xMSG := 'Hello ' + sLineBreak + 'World';
xStrList := TStringList.Create;

// Split(xLineBreak,AnsiString(xMSG),xStrList);
xStrList.Text := xMSG; // <--

xHello := xStrList[0];
xWorld := xStrList[1];
xStrList.Free;
MessageBox(0,PAnsiChar(xHello + xWorld),'Test',0);
end;

关于Delphi - 按换行符分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13667676/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com