Link to home
Start Free TrialLog in
Avatar of liljen_80
liljen_80

asked on

A test on part of the string

I neet too do a test on part of a string:
I have a string lokking like this:
' <Status>8989</Status> and some more'
Then I would like to test if the string contains <Status>, but i do not know how manny whitespaces there are before <Status> and what is after </Status>. What I need is a test too se if <Status> is in the string.

I will also need too get the numbers after <Status>, this result wil I need to have in a list, preferably in a TList.

How too do this?
Avatar of 2266180
2266180
Flag of United States of America image

seems to me you are parsing an xml. why not use an xml parser? txmldocumet at least that comes with delphi.
more articles here: http://delphi.about.com/od/objectpascalide/l/aa072500a.htm
Avatar of kretzschmar
if pos('<Status>',TheString) > 0 then  //found

so

function getStatusValue(Astring : String) : String;
var StartPos, EndPos : Integer;
begin
  StartPos := pos('<Status>',AString);
  if StartPos = 0 then
    raise Exception.Create('No Status there');
  StartPos := StartPos+ Length('<Status>');
  endPos := pos('</Status>',AString);
  result := copy(AString,StartPos,EndPos-StartPos+1);
end;

just from head, i may mistaken soemthing

meikl ;-)
appendix:
xml is overhyped, i'm not a fan of this

meikl ;-)
Avatar of TName
TName

As kretzschmar said, or, if you want to use a TXMLDocument, you can try something like:

procedure TForm1.SearchForStatus;
  procedure IterateNodes(var StrL:TStringList; n:IXMLNode);
  begin
    if n<>nil then
    repeat
      try
        if UpperCase(n.NodeName)='STATUS' then begin
             if n.IsTextElement then begin
               StrL.Add(Trim(n.Text));
             end;
           end;
        if n.ChildNodes<>nil then
           IterateNodes(StrL,n.ChildNodes.First);
      finally
        n := n.NextSibling;
      end;
    until n = nil;
  end;
var
sl:TStringList;
StartN:IXMLNode;
i:Integer;
s:String;
begin
  XmlDoc.LoadFromFile(xmlSource);
  StartN:=XMLDoc.DocumentElement.ChildNodes[0];
  sl:=TStringList.Create;
  sl.Clear;
  IterateNodes(sl,StartN);
  if sl.Count>0 then
    for i:=0 to sl.Count-1 do
      ShowMessage(sl[i]);
end;

Sorry, for some reason I had the impression you wanted to read a whole xml document, but apparently you are really only interested in the string part... But you can still use the Trim() from my example :)
ASKER CERTIFIED SOLUTION
Avatar of liljen_80
liljen_80

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial