[英]Delphi 7 : how to split a string into a TStringList
It's Delphi seven and I need to split a string into lines.这是 Delphi 7 ,我需要将字符串分成几行。
Specifically, I have a DFM as a string (pulled from a MySql database) and I want to split it into lines in a TStringList.具体来说,我有一个 DFM 作为字符串(从 MySql 数据库中提取),我想将它拆分为 TStringList 中的行。
It looks something like this ...它看起来像这样......
'Oject Form1: TScriptForm'#$D#$A' Left = 0'#$D#$A' Top = 0'#$D#$A' Align = alClient'#$D#$A' BorderStyle = bsNone'#$D#$A' ClientHeight = 517'#$D#$A' ClientWidth = 993'#$D#$A' Color = clBtnFace'#$D#$A' Font.Charset = DEFAULT_CHARSET'#$D#$A' Font.Color = clWindowText'#$D#$A' Font.Height = -11'#$D#$A' Font.Name = 'MS Sans Serif''#$D#$A' Font.Style = []'#$D#$A' OldCreateOrder = False'#$D#$A' SaveProps.Strings = ('#$D#$A' 'Visible=False')'#$D#$A' PixelsPerInch = 96'#$D#$A' TextHeight = 13'#$D#$A'
eh诶
Answer: this turned out to be pretty much a non-question for me.回答:这对我来说几乎不是一个问题。 Delphi sees the
#$D#$A
as CR LF automatically, so that all I had to do was assign the string to the Text property of a TStringlist (this also stripped the single quotes from around each #$D#$A
). Delphi 自动将
#$D#$A
视为 CR LF,因此我所要做的就是将字符串分配给 TStringlist 的 Text 属性(这也去除了每个#$D#$A
周围的单引号)。 So, I only had to add a single line of code.所以,我只需要添加一行代码。
If the limiter had not been converted by Delphi then I think that @Roald van Doorn solution would have worked, so he gets awarded the answer.如果 Delphi 没有转换限制器,那么我认为@Roald van Doorn 解决方案会起作用,所以他得到了答案。
It's easy to convert a string to a stringlist, all you need to do is the following steps.将字符串转换为字符串列表很容易,您需要做的就是以下步骤。
Strip the leading '
剥离前导
'
Replace all '#$D#$A'
with #13#10 (thereby splitting the string in to lines again.将所有
'#$D#$A'
替换为 #13#10 (从而将字符串再次拆分为行。
Remove the trailing '#$D#$A
删除尾随
'#$D#$A
Assign the resulting string to the StringList.Text
property, each line in the stringlist is now a line of the DFM file.将生成的字符串分配给
StringList.Text
属性,字符串列表中的每一行现在都是 DFM 文件的一行。
Let's try this code:让我们试试这段代码:
http://www.delphi3000.com/articles/article_4028.asp http://www.delphi3000.com/articles/article_4028.asp
Another thing: i see that you are using TScriptForm object.另一件事:我看到您正在使用TScriptForm对象。 It is good thing that you give to this object serialization/deserialization features.
你给这个对象序列化/反序列化功能是一件好事。
for example, read this metacode:例如,阅读这个元代码:
tscriptform: Myform;
the_stream: TStream;
myform := TScriptForm.create;
the_stream.create(....);
myform.unserialize(the_stream);
In practice: make a TStream descendant that manages serialization of your form, and uses it to encapsulate saving/loading logics of your form objects.在实践中:创建一个管理表单序列化的TStream后代,并使用它来封装表单对象的保存/加载逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.