Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Splitting TstringList items


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Feb 3, 2017 9:40 AM Last Post By: Fred Smith
Fred Smith

Posts: 81
Registered: 12/4/15
Splitting TstringList items  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 1:53 AM
Hi,
I'd like to loop through a TstringList & split any items longer than 10 characters into smaller items without splitting the words:
[0]:One two Three
[1]:Four

becomes
[0]:One two 
[1]:Three
[2]:Four

Which is the most efficient way to achieved this ?
Thank you
Robert Evans

Posts: 114
Registered: 10/16/99
Re: Splitting TstringList items  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 5:21 AM   in response to: Fred Smith in response to: Fred Smith
In article <905399 at forums dot embarcadero dot com>, Fred Smith <?@?.?.invalid>
wrote
I'd like to loop through a TstringList & split any items longer than 10
characters into smaller items without splitting the words:
[0]:One two Three
[1]:Four

becomes
[0]:One two
[1]:Three
[2]:Four

Which is the most efficient way to achieved this ?

Not sure what this has to do with the Delphi IDE but in principle you
need to parse each of the TStringlist Items (that exceeds the maximum
length) by searching for whatever token(s) you define as word
separators.

http://docwiki.embarcadero.com/RADStudio/Berlin/en/Iterating_Through_Strings_in_a_List

The details of the solution will depend to a large degree on exactly
what you mean by "efficient": minimum execution time (for some
particular input data set), minimum memory or minimum programming time -
e.g. by getting someone else to do one's school homework :)

--
Bob Evans
Fred Smith

Posts: 81
Registered: 12/4/15
Re: Splitting TstringList items  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 6:22 AM   in response to: Robert Evans in response to: Robert Evans
I'm using WrapText to split any long strings & store them in my tstringlist. It's working well but wasn't sure if that's the best of doing it:
  MyStringList.Text := WrapText(MyString,10)

Thanks for your help.

Robert Evans wrote:
In article <905399 at forums dot embarcadero dot com>, Fred Smith <?@?.?.invalid>
wrote
I'd like to loop through a TstringList & split any items longer than 10
characters into smaller items without splitting the words:
[0]:One two Three
[1]:Four

becomes
[0]:One two
[1]:Three
[2]:Four

Which is the most efficient way to achieved this ?

Not sure what this has to do with the Delphi IDE but in principle you
need to parse each of the TStringlist Items (that exceeds the maximum
length) by searching for whatever token(s) you define as word
separators.

http://docwiki.embarcadero.com/RADStudio/Berlin/en/Iterating_Through_Strings_in_a_List

The details of the solution will depend to a large degree on exactly
what you mean by "efficient": minimum execution time (for some
particular input data set), minimum memory or minimum programming time -
e.g. by getting someone else to do one's school homework :)

--
Bob Evans
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Splitting TstringList items
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 2:25 PM   in response to: Fred Smith in response to: Fred Smith
Fred wrote:

I'm using WrapText to split any long strings & store them in my
tstringlist. It's working well but wasn't sure if that's the best of
doing it:

MyStringList.Text := WrapText(MyString,10)

I would probably do something more like this instead:

i := 0;
while i < MyStringList.Count do
begin
  if Length(MyStringList[i]) > 10 then
  begin
    tmpList := TStringList.Create;
    try
      tmpList.Text := WrapText(MyStringList[i], 10);
      MyStringList[i] := tmpList[0];
      Inc(i);
      for j := 1 to tmpList.Count-1 do
      begin
        MyStringList.Insert(i, tmpList[j]);
        Inc(i);
      end;
    finally
      tmpList.Free;
    end;
  end else
    Inc(i);
end;


--
Remy Lebeau (TeamB)
Fred Smith

Posts: 81
Registered: 12/4/15
Re: Splitting TstringList items  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2017 9:40 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you very much.

Remy Lebeau (TeamB) wrote:
Fred wrote:

I'm using WrapText to split any long strings & store them in my
tstringlist. It's working well but wasn't sure if that's the best of
doing it:

MyStringList.Text := WrapText(MyString,10)

I would probably do something more like this instead:

i := 0;
while i < MyStringList.Count do
begin
  if Length(MyStringList[i]) > 10 then
  begin
    tmpList := TStringList.Create;
    try
      tmpList.Text := WrapText(MyStringList[i], 10);
      MyStringList[i] := tmpList[0];
      Inc(i);
      for j := 1 to tmpList.Count-1 do
      begin
        MyStringList.Insert(i, tmpList[j]);
        Inc(i);
      end;
    finally
      tmpList.Free;
    end;
  end else
    Inc(i);
end;


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02