Solved

Parsing a String

Posted on 2003-11-05
6
437 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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