Solved

Parsing a String

Posted on 2003-11-05
6
442 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
Industry Leaders: 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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