Adding items dynamically in ClientDataSet

0

How can I make the text of a% colon separated by a semicolon (;) as a record in TMemo .

Example:

  

Code, Category, Phrase, Author

     

1; Motivational; Phrase 1; Author 1

     

2; Proverb; Sentence 2; Author 2

     

3; Dictation; Sentence 3; Unknown

What I'm finding more complicated is to "trim" the String at every semicolon (;) and read it later.

I think the code will follow this logic:

for Linha := 0 to Memo1.Lines.Count-1 do
begin
  CDS.Insert;
  CDSCod.AsString       := {Código}
  CDSCategoria.AsString := {Categoria}
  CDSFrase.AsString     := {Frase} 
  CDSAutor.AsString     := {Autor}
  CDS.Post;
end;
    
asked by anonymous 11.12.2018 / 00:33

1 answer

3

You can use the String helper for this, declare in uses the System.StrUtils . This way you have access to various help functions.

For the case presented you would use:

CDS.Insert;
CDSCod.AsString       := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSCategoria.AsString := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSFrase.AsString     := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSAutor.AsString     := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDS.Post;

If the dataset in question you are using does not have a defined index you can change the .Insert by .Append to gain in performance.

Edit: Remembering that as String is an array, posicao_especifica starts with 0.

    
11.12.2018 / 11:12