Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse som text

Status
Not open for further replies.

YamahaJojje

Programmer
May 27, 2004
11
SE
Hi

How can I parse this textfield=Test&textfield2=Test2&Submit=Submit

so Textfield turns to a variabel with the value Test and
Textfiled2 gets the value Test2?

I can't get it to work :(

Thx in advance
 
Unfortunately I don't currently have Delphi installed on any of my machines, so I can't safely give you any working code.

However I think the easiest way to do this would be to create a dynamic array, adjusting its size as each string is added using SetLength, I think. Parse the string with the Pos function (I can't remember the syntax) but you will be looking for the next occurence '='. Use a separate variable to keep the current Position so that you can use that as the start point for the next search. To extract the value portion you would use a second Pos to search for '&'. I think Pos returns 0 if no match is found so you can use that to control your WHILE or REPEAT loop.

Although this won't give you the variable names that you requested it will give you arrayVar[0] equalling 'Test' and arrayVar[1] equalling 'Test2'.

If you need to store the variable name, then you could use a two-dimensional array.
 
Try this:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  TS: TStrings;
begin
  TS := TStringList.Create;
  TS.Delimiter := '&';
  TS.DelimitedText := 'textfield=Test&textfield2=Test2&Submit=Submit';

  ShowMessage(TS.Values['textfield2']);

  TS.Free;
end;

KungTure-RX.jpg

//Nordlund
 
Nice code Nordlund! A couple of extra points:
1) I would add a try..finally construct to ensure that the memory allocated for TS is released no matter what:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  TS: TStringList;
begin
  TS := TStringList.Create;
  try
    TS.Delimiter := '&';
    TS.DelimitedText := 'textfield=Test&textfield2=Test2&Submit=Submit';

    ShowMessage(TS.Values['textfield2']);
  finally
    TS.Free;
  end;
end;
2) If you wanted to display this information in a grid, drop a TValueListEditor (found in the "Additional" tab of Delphi 6) on a form and use the following code:
Code:
procedure TForm1.Button5Click(Sender: TObject);
var
  TS: TStringList;
  i: Integer;
begin
  TS := TStringList.Create;
  try
    TS.Delimiter := '&';
    TS.DelimitedText := 'textfield=Test&textfield2=Test2&Submit=Submit';
    for i := 0 to TS.Count - 1 do
      ValueListEditor1.InsertRow(TS.Names[i], TS.Values[TS.Names[i]], True);
  finally
    TS.Free;
  end;
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
He has to code something by himself, eh? ;-)

KungTure-RX.jpg

//Nordlund
 
Sorry, I was getting a bit enthusiastic! [wink]

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
and then theres the 'old chestnut' about free on its own not being sufficient to release the memory taken up by a stringlist. which should be emptied before freeing
Freenadnil(TS); perhaps

earthandfire [wink]


Steve

Life is like a Grapefruit, sort of orangey-yellow and dimpled on the outside, wet and squidgy in the middle, it's got pips inside too. Oh and some people have half a one for breakfast. Ford Prefect.

Want to do more with TGML Download Star
 
I'd go for "FreeAndNil(TS);" rather than "FreeNadNil(TS);" Steve [wink]

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Steve,

As I understand it, FreeAndNil does not do any more than set the pointer to Nil when the object is freed. It doesn't go around freeing the memory taken up by the strings in a TStringList.

Here is the FreeAndNil routine in (Delphi 7) SysUtils:
Code:
procedure FreeAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  Temp.Free;
end;
I haven't looked at the code but I am sure that TStringList frees the memory used by the strings in its destructor.

Or have I missed the point of your comment?

Andrew
Hampshire, UK
 
Be aware that the .DelimitedText method does not limit itself to the specified delimiter. It also delimits when a space is found. So if your string could contain a space, you might consider replacing all spaces with some other character. I use a "hard space" Chr(160). (The same character you get in Word with Ctrl+Shift+Space.)

something like this:
Code:
const
  HARD_SPACE = Chr(160);
:
:
s := StringReplace( s, ' ',HARD_SPACE,[rfReplaceAll]);
:
:
 
one small remark about freeandnil, the nil part is just to make sure that the freed pointer value is really nil because by only calling free, the object pointer will be whatever value and if you would check it with the Assigned() function, it will pass....

--------------------------------------
What You See Is What You Get
 
I have alway been led to belive that (proir to freenandnil)
you should do

for a := 0 to length(stringlist)-1 do
stringlist.strings[a] := '';
StringList.free;

Because the memory is NOT freed properly in the destructor.
But there always seems to be debate about this.

Look, I am starting to get horrible flashbacks of garbage collection delays on the C64.





Steve

Life is like a Grapefruit, sort of orangey-yellow and dimpled on the outside, wet and squidgy in the middle, it's got pips inside too. Oh and some people have half a one for breakfast. Ford Prefect.

Want to do more with TGML Download Star
 
Steve, I've not come across this before. I always thought that it was sufficient to use Free/FreeAndNil. Can you cite an official Borland source that says this?

I've never noticed memory leaks as a result of not clearing all strings in a stringlist.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I think I first came across this in a Magazine tutorial. I will see if I can dig it out.


Steve

Life is like a Grapefruit, sort of orangey-yellow and dimpled on the outside, wet and squidgy in the middle, it's got pips inside too. Oh and some people have half a one for breakfast. Ford Prefect.

Want to do more with TGML Download Star
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top