Solved

Getting a piece of text out of other text

Posted on 2004-10-09
5
188 Views
Last Modified: 2013-12-03
Hey there,

I have some text and I want to copy some parts out of it.
Basically, the text is this.

<a href="#" onclick="return !showPopup(th_name, event,Persons name.');">?</a> <font color="darkblue">Name:</font> <div align="right">Robert&nbsp;</div>
<a href="#" onclick="return !showPopup(th_adddy, event,'addy.');">?</a> <font color="darkblue">Address:</font> <div align="right">Over rainbow&nbsp;</div>
<a href="#" onclick="return !showPopup(th_ph, event,personsphone.');">?</a> <font color="darkblue">Phone:</font> <div align="right">8751nbsp;</div>

So what I would want to do, if located the Name and then get there actual name. So is the best way to get postion of "Name:</font> <div align="right">" then get the characters between that and & - so i'd end up with Robert.....

Is that the best way?
0
Comment
Question by:wildzero
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
sftweng earned 75 total points
ID: 12265868
unit uQ_21161984;

interface

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

type
  TForm1 = class(TForm)
    EditSource: TEdit;
    EditStart: TEdit;
    EditStop: TEdit;
    EditResult: TEdit;
    ButtonExtract: TButton;
    procedure ButtonExtractClick(Sender: TObject);
    function StringExtract(aSource, aStartDelim, aStopDelim : String) : String;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.StringExtract(aSource, aStartDelim, aStopDelim : String) : String;
var
  workString : String;
  resultString : String;
  startPos, stopPos : Integer;
begin
  workString := aSource;
  resultString := '';
  startPos := Pos(aStartDelim,workString);
  if (startPos > 0)
  then begin
    Delete(workString,1,startPos+Length(aStartDelim)-1);
    stopPos := Pos(aStopDelim,workString);
    if (stopPos > 0)
    then begin
      workString := Copy(workString,1,stopPos-1);
    end; {if}
    resultString := workString;
  end {if};
  StringExtract := workString;
end; {StringExtract}

procedure TForm1.ButtonExtractClick(Sender: TObject);
var
  str : String;
begin
  EditResult.Text := StringExtract(EditSource.Text,
                                   EditStart.Text,
                                   EditStop.Text);
end {ButtonExtractClick};

end.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12265876
Oops - last line of String Extract should be:
  StringExtract := resultString;
0
 
LVL 10

Author Comment

by:wildzero
ID: 12269640
Hey sftweng,

Sorry for taking so long to get back to you.
That worked ok, except - for my text I provided what should I use for start/stop delimters?

Because having a start delimter of > will be totally unsuitable, and using 'Name:</font> <div align="right">' just returns nothing.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12269721
Here's the *.dfm I used:

object Form1: TForm1
  Left = -2
  Top = 110
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object EditSource: TEdit
    Left = 64
    Top = 40
    Width = 497
    Height = 21
    TabOrder = 0
    Text =
      '<a href="#" onclick="return !showPopup(th_name, event,Persons na' +
      'me.'#39');">?</a> <font color="darkblue">Name:</font> <div align="ri' +
      'ght">Robert&nbsp;</div>'
  end
  object EditStart: TEdit
    Left = 64
    Top = 72
    Width = 497
    Height = 21
    TabOrder = 1
    Text = 'Name:</font> <div align="right">'
  end
  object EditStop: TEdit
    Left = 64
    Top = 104
    Width = 497
    Height = 21
    TabOrder = 2
    Text = '&nbsp'
  end
  object EditResult: TEdit
    Left = 64
    Top = 136
    Width = 497
    Height = 21
    TabOrder = 3
  end
  object ButtonExtract: TButton
    Left = 64
    Top = 168
    Width = 75
    Height = 25
    Caption = 'Extract'
    TabOrder = 4
    OnClick = ButtonExtractClick
  end
end
0
 
LVL 10

Author Comment

by:wildzero
ID: 12273861
Ah how right you were

Reason it didn't work for me was cos of WordWrap on the Tmemo.
Sorted now - many thanks!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

806 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