?
Solved

Parsing a String

Posted on 2003-11-05
6
Medium Priority
?
443 Views
Last Modified: 2008-02-01
Hello, I hvae a String with following format (From IdImap4 Mailboxlist:Mailbox/Sub) XXXX/YYYY, I would get the left Side in Sting1, and right side String 2. Could you help me there?
0
Comment
Question by:k4hvd77
[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
6 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9685374
k4hvd77,
  Can you explain what is your criteria here for the left side and right side?
...Snehanshu
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 9685470
I suppose you could use the Pos-Function to determine the position of the 'Break'-Character.
Example :

- Place 1 TButton and 2 TEdits on a Form.
- In the private section of the unit declare following function :
      function SplitString( SplitChar: String ; Source:String ; var Part1:String ; var Part2: String ): Integer;

- Put following code in the OnClick-Event of the button :

procedure TForm1.Button1Click(Sender: TObject);
var
  strSource : String;
  strP1, strP2 : String;
begin
  strSource  := '(From IdImap4 Mailboxlist:Mailbox/Sub) XXXX/YYYY';
  SplitString(')', strSource, strP1, strP2);
  Edit1.Text := strP1;
  Edit2.Text := strP2;
end;



function TForm1.SplitString( SplitChar: String ; Source: String; var Part1, Part2: String ): Integer;
begin
  Result := Pos(SplitChar, Source);
  Try
        Part1 := Copy( Source, 1, Result );
        Part2 := Copy( Source, Result+1, Length(Source) );
  except
    Result := 0;
  end;
end;


Hope te be of some help
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9685489
something like this?

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
var s1, s2 : string;
begin
  //discard front of 'From IdImap4 Mailboxlist:Mailbox/Sub'
  //containd in edit1
  s1 := edit1.Text;
  splitstr(s1,':');
  //s1 contains now 'Mailbox/Sub'
  //get the front part
  s2 := splitstr(s1,'/');
  //s2 contains now front part, s1 the rest
  edit2.Text := s2;  //fronpartdisplay
  edit3.Text := s1;  //rest display

end;

end.

meikl ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:DeerBear
ID: 9685831
Hi,

If you have Delphi6, you can use TStringList with DelimitedText to
split the string.

HTH,

Andrew
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 600 total points
ID: 9690356
Try this real cool technique...Very FAST, too.

var
  MyStr: String;
  MyParser: TParser;
  MS: TMemoryStream;
begin
  MyStr := '(From IdImap4 Mailboxlist:Mailbox/Sub) XXXX/YYYY';
  MS := TMemoryStream.Create;
  MS.Position := 0;
  MS.Write(MyStr[1], Length(MyStr));
  MS.Position := 0;
  MyParser := TParser.Create(MS);
  MyStr := MyParser.TokenString;
  while MyParser.Token <> toEOF do
  begin
    if MyParser.TokenSymbolIs(MyParser.TokenString) then
    begin
      MyStr := MyParser.TokenString;
      ShowMessage(MyStr);
    end;
    MyParser.NextToken;
  end;
  MyParser.Free;
  MS.Free;
end;
0
 
LVL 6

Expert Comment

by:swift99
ID: 9691844
slashLoc := pos('/', inputString);
LeftString := Copy (inputString, 1, pos-1);
RightString := Copy (inputString, pos+1, length (inputString)-pos);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

741 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