Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String parsing question

Posted on 2002-04-27
9
Medium Priority
?
253 Views
Last Modified: 2010-04-04
I need to parse a string like this:

'9'#$D#$A'[Status]'#$A#$D#$A'e'#$D#$A'Curr=3006'#$A#$D#$A'13'#$D#$A'Update=64'#$A#$D#$A'b'#$D#$A'STOP=1'#$A#$D#$A'b'#$D#$A'PLAY=0'#$A#$D#$A'c'#$D#$A'PAUSE=0'#$A#$D#$A'd'#$D#$A'RANDOM=0'#$A#$D#$A'c'#$D#$A'GROUP=0'#$A#$D#$A'b'#$D#$A'MUTE=0'#$A#$D#$A'd'#$D#$A'REPEAT=0'#$A#$D#$A'f'#$D#$A'State=Inactive'#$A#$D#$A'd'#$D#$A'[End Status]'#$A#$D#$A'2'#$D#$A#$D#$A#$D#$A'0'#$D#$A#$D#$A

and need to get the "key=value" strings such as Update=64. The keys are constants but the values are not. I think this is an easy one but it has been a while for me and I am lazy.
0
Comment
Question by:mdlittle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:lopem
ID: 6973376
MdLittle,

Im not very sure what is what you want. In any case, every parsing problem I have I use this unit. Maybe this can be helpful.

best regards
Manuel Lopez (lopem)

{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
{         TechnoJocks Turbo Toolkit v4.00            Released: Feb 1, 1988    }
{                                                                             }
{         Module: StrngTTT    --    string manipulation routines              }
{                                                                             }
{                       Copyright R. D. Ainsbury (c) 1986                     }
{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}

unit StrngTTT;

interface

Function PadLeft(Str:string;Size:byte;Pad:char):string;
Function PadCenter(Str:string;Size:byte;Pad:char):string;
Function PadRight(Str:string;Size:byte;Pad:char):string;
Function Last(N:byte;Str:string):string;
Function First(N:byte;Str:string):string;
Function Upper(Str:string):string;
Function Lower(Str:string):string;
Function Proper(Str:string):string;
Function OverType(N:byte;StrS,StrT:string):string;
Function Strip(L,C:char;Str:string):string;
Function LastPos(C:Char;Str:string):byte;
Function PosWord(Wordno:byte;Str:string):byte;
Function WordCnt(Str:string):byte;
Function ExtractWords(StartWord,NoWords:byte;Str:string):string;
Function Str_to_Int(Str:string):integer;
Function Real_to_str(Number:real;Decimals:byte):string;
Function Int_to_Str(Number:longint):string;

implementation

Function PadLeft(Str:string;Size:byte;Pad:char):string;
var temp : string;
begin
    Fillchar(Temp[1],Size,Pad);
    SetLength(Temp,Size);
    If Length(Str) <= Size then
       Move(Str[1],Temp[1],length(Str))
    else
       Move(Str[1],Temp[1],size);
    PadLeft := Temp;
end;

Function PadCenter(Str:string;Size:byte;Pad:char):string;
var temp : string;
L : byte;
begin
    Fillchar(Temp[1],Size,Pad);
    SetLength(Temp,Size);
    L := length(Str);
    If L <= Size then
       Move(Str[1],Temp[((Size - L) div 2) + 1],L)
    else
       Move(Str[((L - Size) div 2) + 1],Temp[1],Size);
    PadCenter := temp;
end; {center}

Function PadRight(Str:string;Size:byte;Pad:char):string;
var
  temp : string;
  L : integer;
begin
    Fillchar(Temp[1],Size,Pad);
    SetLength(Temp,Size);
    L := length(Str);
    If L <= Size then
       Move(Str[1],Temp[succ(Size - L)],L)
    else
       Move(Str[1],Temp[1],size);
    PadRight := Temp;
end;

Function Last(N:byte;Str:string):string;
var Temp : string;
begin
    If N > length(Str) then
       Temp := Str
    else
       Temp := copy(Str,succ(length(Str) - N),N);
    Last := Temp;
end;  {Func Last}

Function First(N:byte;Str:string):string;
var Temp : string;
begin
    If N > length(Str) then
       Temp := Str
    else
       Temp := copy(Str,1,N);
    First := Temp;
end;  {Func First}

Function Upper(Str:string):string;
var
  I : integer;
begin
    For I := 1 to length(Str) do
        Str[I] := Upcase(Str[I]);
    Upper := Str;
end;  {Func Upper}

Function Lower(Str:string):string;
var
  I : integer;
begin
    For I := 1 to length(Str) do
        If ord(Str[I]) in [65..90] then
           Str[I] := chr(ord(Str[I]) + 32);
    Lower := Str;
end;  {Func Lower}

Function Proper(Str:string):string;
var
  I : integer;
  SpaceBefore: boolean;
begin
    SpaceBefore := true;
    Str := lower(Str);
    For I := 1 to length(Str) do
        If SpaceBefore and (ord(Str[I]) in [97..122]) then
        begin
            SpaceBefore := False;
            Str[I] := Upcase(Str[I]);
        end
        else
            If (SpaceBefore = False) and (Str[I] = ' ') then
                SpaceBefore := true;
    Proper := Str;
end;

Function OverType(N:byte;StrS,StrT:string):string;
{Overlays StrS onto StrT at Pos N}
var
  L : byte;
  StrN : string;
begin
    L := N + pred(length(StrS));
    If L < length(StrT) then
       L := length(StrT);
    If L > 255 then
       Overtype := copy(StrT,1,pred(N)) + copy(StrS,1,255-N)
        else
    begin
       Fillchar(StrN[1],L,' ');
       SetLength(StrN,L);
       Move(StrT[1],StrN[1],length(StrT));
       Move(StrS[1],StrN[N],length(StrS));
       OverType := StrN;
    end;
end;  {Func OverType}

Function Strip(L,C:char;Str:string):string;
{L is left,center,right,all,ends}
var I :  byte;
begin
    Case Upcase(L) of
    'L' : begin       {Left}
              While Str[1] = C do
                    Delete(Str,1,1);
          end;
    'R' : begin       {Right}
              While Str[length(Str)] = C do
                    Delete(Str,length(Str),1);
          end;
    'B' : begin       {Both left and right}
              While Str[1] = C do
                    Delete(Str,1,1);
              While Str[length(Str)] = C do
                    Delete(Str,length(Str),1);
          end;
    'A' : begin       {All}
              I := 1;
              Repeat
                   If Str[I] = C then
                      Delete(Str,I,1)
                   else
                      I := succ(I);
              Until (I > length(Str)) or (Str = '');
          end;
    end;
    Strip := Str;
end;  {Func Strip}

Function LastPos(C:Char;Str:string):byte;
Var I : byte;
begin
    I := succ(Length(Str));
    Repeat
         I := Pred(I);
    Until (I = 0) or (Str[I] = C);
    LastPos := I;
end;  {Func LastPos}

Function LocWord(StartAT,Wordno:byte;Str:string):byte;
{local proc used by PosWord and Extract word}
var
  W,L: integer;
  Spacebefore: boolean;
begin
    If (Str = '') or (wordno < 1) or (StartAT > length(Str)) then
    begin
        LocWord := 0;
        exit;
    end;
    SpaceBefore := true;
    W := 0;
    L := length(Str);
    StartAT := pred(StartAT);
    While (W < Wordno) and (StartAT <= length(Str)) do
    begin
        StartAT := succ(StartAT);
        If SpaceBefore and (Str[StartAT] <> ' ') then
        begin
            W := succ(W);
            SpaceBefore := false;
        end
        else
            If (SpaceBefore = false) and (Str[StartAT] = ' ') then
                SpaceBefore := true;
    end;
    If W = Wordno then
       LocWord := StartAT
    else
       LocWord := 0;
end;

Function PosWord(Wordno:byte;Str:string):byte;
begin
    PosWord := LocWord(1,wordno,Str);
end;  {Func Word}

Function WordCnt(Str:string):byte;
var
  W,I: integer;
  SpaceBefore: boolean;
begin
    If Str = '' then
    begin
        WordCnt := 0;
        exit;
    end;
    SpaceBefore := true;
    W := 0;
    For  I :=  1 to length(Str) do
    begin
        If SpaceBefore and (Str[I] <> ' ') then
        begin
            W := succ(W);
            SpaceBefore := false;
        end
        else
            If (SpaceBefore = false) and (Str[I] = ' ') then
                SpaceBefore := true;
    end;
    WordCnt := W;
end;

Function ExtractWords(StartWord,NoWords:byte;Str:string):string;
var Start, finish : integer;
begin
    If Str = '' then
    begin
        ExtractWords := '';
        exit;
    end;
    Start := LocWord(1,StartWord,Str);
    If Start <> 0 then
       finish := LocWord(Start,succ(NoWords),Str)
    else
    begin
        ExtractWords := '';
        exit;
    end;
    If finish <> 0 then
       Repeat
           finish := pred(finish);
       Until Str[finish] <> ' '
    else
       finish := length(Str);
    ExtractWords := copy(Str,Start,succ(finish-Start));
end;  {Func ExtractWords}

Function Int_to_Str(Number:longint):string;
var Temp : string;
begin
    Str(Number,temp);
    Int_to_Str := temp;
end;

Function Str_to_Real(Str:string):real;
var temp,code : integer;
begin
    If length(Str) = 0 then
       Str_to_Real := 0
    else
    begin
        If Copy(Str,1,1)='.' Then
           Str:='0'+Str;
        If (Copy(Str,1,1)='-') and (Copy(Str,2,1)='.') Then
           Insert('0',Str,2);
        If Str[length(Str)] = '.' then
           Delete(Str,length(Str),1);
       val(Str,temp,code);
       if code = 0 then
          Str_to_Real := temp
       else
          Str_to_Real := 0;
    end;
end;

function Real_to_str(Number:real;Decimals:byte):string;
var Temp : string;
begin
    Str(Number:20:Decimals,Temp);
    repeat
         If copy(Temp,1,1) = ' ' then delete(Temp,1,1);
    until copy(temp,1,1) <> ' ';
    Real_to_Str := Temp;
end;

Function  Str_to_Int(Str:string):integer;
var temp,code : integer;
begin
    If length(Str) = 0 then
       Str_to_Int := 0
    else
    begin
       val(Str,temp,code);
       if code = 0 then
          Str_to_Int := temp
       else
          Str_to_Int := 0;
    end;
end;

end.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 6973454
var s : string;
    KeyPosn : integer;
    ValuePosn : integer;
    DelimPosn : integer;
    sKeyValue : string;
    iKeyValue : integer;

// the following assignment statement is not syntactically correct
s:='9'#$D#$A'[Status]'#$A#$D#$A'e'#$D#$A'Curr=3006'#$A#$D#$A'13'#$D#$A'Update=64'#$A#$D#$A'b'#$D#$A'STOP=1'#$A#$D#$A'b'#$D#$A'PLAY=0'#$A#$D#$A'c'#$D#$A'PAUSE=0'#$A#$D#$A'd'#$D#$A'RANDOM=0'#$A#$D#$A'c'#$D#$A'GROUP=0'#$A#$D#$A'b'#$D#$A'MUTE=0'#$A#$D#$A'd'#$D#$A'REPEAT=0'#$A#$D#$A'f'#$D#$A'State=Inactive'#$A#$D#$A'd'#$D#$A'[End
Status]'#$A#$D#$A'2'#$D#$A#$D#$A#$D#$A'0'#$D#$A#$D#$A ;

KeyPosn:=Pos('Curr=',s);
ValuePosn:=KeyPosn + 4;  //Length('Curr=') = 4
s := Copy(s, ValuePosn, Length(s));
DelimPosn := Pos('''',s);
iKeyValue := StrToInt(Copy(s, 1, DelimPosn));

KeyPosn:=Pos('State=',s);
ValuePosn:=KeyPosn + 6;  //Length('State=') = 6
s := Copy(s, ValuePosn, Length(s));
DelimPosn := Pos('''',s);
sKeyValue := Copy(s, 1, DelimPosn);

Note: I've provided you with code to parse an integer value and a string value.  I leave it to you to add the other key=value parse code snippets and save them in the appropriate places.

good luck
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6973464
load or assign (text-property) it into a tstringlist

you could code a function like this

var
  sl : tstringlist;
  result : String;
begin
  sl := tstringlist.create;
  try
    try
      sl.text := 'YousrString Above';
      result := sl.Value[sl.IndexOfName('YourKeyString')];
    finally  
      sl.free;
  except
    showmessage('An Error');
    raise:
  end;
end;

hope this helps

meikl ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6973550
sorry there where much typos above

a tested sample

unit sl_value_u;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

function getvalue(akey : String) : String;
var
 sl : tstringlist;
begin
 sl := tstringlist.create;
 try
   try
     sl.text := '9'#$D#$A'[Status]'#$A#$D#$A'e'#$D#$A'Curr=3006'#$A#$D#$A'13'#$D#$A'Update=64'#$A#$D#$A'b'#$D#$A'STOP=1'#$A#$D#$A'b'#$D#$A'PLAY=0'#$A#$D#$A'c'#$D#$A'PAUSE=0'#$A#$D#$A'd'#$D#$A'RANDOM=0'#$A#$D#$A'c'#$D#$A'GROUP=0'#$A#$D#$A'b'#$D#$A'MUTE=0'#$A#$D#$A'd'#$D#$A'REPEAT=0'#$A#$D#$A'f'#$D#$A'State=Inactive'#$A#$D#$A'd'#$D#$A'[EndStatus]'#$A#$D#$A'2'#$D#$A#$D#$A#$D#$A'0'#$D#$A#$D#$A;
     result := sl.Values[Akey];
   finally
     sl.free;
   end;
 except
   showmessage('An Error');
   raise;
 end;
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
  edit2.text := getValue(edit1.text);
end;

end.

meikl ;-)
0
 
LVL 46

Expert Comment

by:aikimark
ID: 6973772
meikl,

Thanks for teaching me something I didn't know.  Your answer is prefered.
0
 

Author Comment

by:mdlittle
ID: 6975208
kretzschmar:

Your solution worked very nicely. Thanks for the help. Let me ask you another related question. How would I parse the following:

[sample 1]
key=value
key2=value2
key3=value3
[end sample 1]

[sample 2]
key=value
key2=value2
key3=value3
[end sample 2]


I need to key=value for each sample. The keys are constants.
0
 

Author Comment

by:mdlittle
ID: 6975209
kretzschmar:

I forgot the string is loaded with lots of control character as was the first string above. Thanks for your help. Do you want the points?? Please make your next comment an answer so I can award the points.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 2000 total points
ID: 6988879
>Do you want the points??
yep, i will

sorry for delay, had just forgotten this q,
have just reviewed my notifications

following a two way sample

unit get_section_value_u;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;  //beginsection text
    Edit2: TEdit;  //endsection text
    Edit3: TEdit;  //key
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


//one possibilty (from many)
function getvalue(content : TStringlist; beginsectiontoken, endsectiontoken, Akey : String) : String;
var
  sl : tstringlist;
  b, e, i : Integer;
begin
  result := '';
  if assigned(content) then //only if a content is given
  begin
    b := content.indexOf(beginsectiontoken);  //get range of section
    e := content.IndexOf(endsectiontoken);
    if (b > -1) and (b < e) then  //valid range
    begin
      sl := tstringlist.create;
      try
        for I := b to e do
          sl.add(content[i]);
        result := sl.Values[Akey];
      finally
        sl.free;
      end;
    end;
  end;
end;

//sample by hand, sections must include the []
procedure TForm1.Button1Click(Sender: TObject);
var sl : TStringlist;
begin
  sl := tstringlist.create;
  try
    sl.add('[sample 1]');  //supply content
    sl.add('key=1value');
    sl.add('key2=1value2');
    sl.add('key3=1value3');
    sl.add('[end sample 1]');
    sl.add('');
    sl.add('[sample 2]');
    sl.add('key=2value');
    sl.add('key2=2value2');
    sl.add('key3=2value3');
    sl.add('[end sample 2]');
    showmessage(getvalue(sl, edit1.text, edit2.text, edit3.text)); //getvalue
  finally
    sl.free;
  end;
end;

//if you have a file, section may not have the []
procedure TForm1.Button2Click(Sender: TObject);
var
  inifile : TInifile;  //the unit inifiles is used in the uses clause
begin
  inifile := tinifile.create('c:\MyFile.Dat');  //open the file
  try
    //getvalue, only section and key must be given, without []
    showmessage(inifile.ReadString(edit1.text,edit3.text,'Not Found'));
  finally
    inifile.free;
  end;
end;

end.


meikl ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

671 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