Solved

delphi parse string to params

Posted on 2016-10-02
3
81 Views
Last Modified: 2016-10-02
i have this string

oops=Doone&name=data&count=10&ram=otherdata

i want to parse it to parameters as example

params[1] = oops
Params[2] = doone
Params[3] = name
Params[4] = data
Params[5] = count
Params[6] = 10
Params[7] = ram
Params[8] = otherdata

how can i do that ? in delphi
0
Comment
Question by:dolphin King
3 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41825442
Since you have two different delimiters, you should use the regular TRegex expression class.

References and examples:
http://docwiki.embarcadero.com/CodeExamples/Berlin/en/RTL.RegExpressionVCL_Sample
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Regular_Expressions
0
 
LVL 3

Expert Comment

by:lopem
ID: 41825589
A quick and dirty solution: I put a button and a edit component. I am using the strngttt.pas library (and old one for turbo pascal, but works in practically every version of delphi). You can find it here: http://files.mpoli.fi/unpacked/software/programm/general/tttsrc51.zip/strnttt5.pas

So, this is my code. I am asumming you´re using these delimiters that are not alphabet characters.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, strngttt, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var
    x       : integer;
    InpStr  : string;
    C       : char;
    TextS   : string;
begin
  InpStr := Edit1.Text;
  for x := 1 to length(InpStr) do
  begin
    //get rid of all these undesired chars
    C := InpStr[x];
    if ((ord(C) < 65) or (ord(C) > 122)) then InpStr[x] := ' ';
  end;
    //now display every word
  for i := 1 to WordCnt(InpStr) do
  begin
    TextS := '';
    TextS := ExtractWords(i,1,InpStr);
    ShowMessage(TextS);
  end;
end;

end.
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 41825680
Using TStringList (which handle delimiters) and use internal feature to handle value pairs:
lst: TStringList;
begin
  lst := TStringList.Create;
  try
    lst.Delimiter := '&';  //first split by &
    lst.DelimtedText := 'oops=Doone&name=data&count=10&ram=otherdata';
    
    //access values by value names
    Param1 := lst.Values['oops'];  //get value Doone
    Param2 := lst.Values['name'];  //get value data
    Param3 := lst.Values['count'];  //get value 10   
    Param3 := lst.Values['ram'];  //get value otherdata  
  finally
    lst.Free;
  end;
end;

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
How to debug For loops? 3 33
What is Python programming? 3 62
parse convert xml feed to text (python) 2 28
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This is about my first experience with programming Arduino.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now