Solved

Parsing a String

Posted on 2003-11-05
6
441 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
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 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 150 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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