More on strings (hey rllLibby!)

Respektable
Respektable used Ask the Experts™
on
Damn.. locked out my last question.

Ok... so adding to a stringlist is very, very fast.  But how can I get a pure string back out of a stringlist, without all the control characters the stringlist adds when I read the "text" property. Forget commatext property. That is very, very slow.


SL.add('ID1');
Sl.add('ID2');

MyDesiredResult = 'ID1 delimiter ID2 delimiter...etc

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
why not just replace the control characters with your delimiter

Author

Commented:
I was just working on that... but here's the problem

I can't seem to POS for the control characters

They read in the tracer as '#$D#$A'.  But I can't seem to use pos to find them. Why is that? Aren't they just ascii characters?

Author

Commented:
Oh jesus.. I was putting them in single quotes.  Never mind
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:
From your last question:

The reason why IndexOfName is so slow is that it walks from first -> last item. So the search time is directly related to (1) the number of items (2) where the search item falls in the list.

This could be made faster, but it would require sorting the list. The problem with that is the items will be moved around, and it sounds like the index is important to you.

A solution to this would be to use a sorted TStringList, but when you perform the add, you use AddObject(), that way you can maintain the natural order, ex:

List.AddObject(Name, Pointer(List.Count))
List.AddObject(Name, Pointer(List.Count))
List.AddObject(Name, Pointer(List.Count))

The Find function could then be used (it performs a split search) to get a starting point in the list, which you could then perform your checks on. A side note on this; better to keep list unsorted while adding large numbers of items, due to the shifting that would take place (memory copies)

Or, you could use the Pos function like you have above based on the List.Text property,

Hope this helps,
Russell
Software Engineer, Advisory
Top Expert 2005
Commented:
In regards to the question at the top, getting text without sep chars.

function StringListToString(List: TStringList): String;
var  nSize:      Integer;
     nCount:     Integer;
     s:          String;
     p:          PChar;
begin

  // Set default result
  result:='';

  // Quick check
  if (List = nil) or (List.Count = 0) then exit;

  // Determine total length needed
  nSize:=0;
  for nCount:=0 to List.Count-1 do Inc(nSize, Length(List[nCount]));

  // Allocate result string
  SetString(result, nil, nSize);
  p:=PChar(result);

  // Move the strings over
  for nCount:=0 to List.Count-1 do
  begin
     s:=List[nCount];
     nSize:=Length(s);
     if (nSize = 0) then Continue;
     System.Move(Pointer(S)^, p^, nSize);
     Inc(p, nSize);
  end;

end;

----

Russell

Author

Commented:
Sheesh.. thanks.... that's a lot of work. I have opted to just use the Stringlist's control chars as my delimiter. To hell with it.. lol.  Not worth it to save a byte or two

Author

Commented:
Not many points... but thanks. And thank you too Kretz.
Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:

Anytime...
Not a big deal either, just a hacked version of TStringLists.GetTextStr function.

Russell


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial