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?
Delphi

Avatar of undefined
Last Comment
liljen_80

8/22/2022 - Mon
2266180

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
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 ;-)
kretzschmar

appendix:
xml is overhyped, i'm not a fan of this

meikl ;-)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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;

TName

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
liljen_80

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question