Solved

Getting a piece of text out of other text

Posted on 2004-10-09
5
184 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now