Solved

finding CR/LF in a text string

Posted on 2002-05-22
14
1,225 Views
Last Modified: 2010-04-04
Hi...
   My app reads in lines of text strings. I want to be able to tell in a quick and efficient way (ie. without slowing down my app too much) whether or not there is a carriage return and/or line feed in the string I read in.

Any suggestions?

Thanks
   Shawn
0
Comment
Question by:aztec
[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
14 Comments
 
LVL 1

Expert Comment

by:thegroup
ID: 7027992
Pos (String,#13), if this return any value >= 0 the character #13 (cr) exists. The same for (lf = #10)
0
 
LVL 1

Expert Comment

by:MBo
ID: 7028707
CRLFPos:=pos(#13#10,YourString);
0
 

Author Comment

by:aztec
ID: 7030730
MBo - it's not working properly. I submit to it a text string that is terminated by a cr/lf and the

pos(#13#10, YourString);

statement returns 0. It's not seeing the cr/lf.

Shawn
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!

 

Author Comment

by:aztec
ID: 7030733
sorry, false alarm, I had the arguments in the Pos statement reversed. Duhhhh.....
0
 

Author Comment

by:aztec
ID: 7030737
nope, I was right the first time - even with the arguments in the right order in the Pos statemenet, it's still not picking up the #13#10. Please advise.

Shawn
0
 

Author Comment

by:aztec
ID: 7030827
sorry, false alarm, I had the arguments in the Pos statement reversed. Duhhhh.....
0
 

Author Comment

by:aztec
ID: 7031183
did a re-load, that's why there's a duplicate posting from me. Sorry.

MBo, your suggestion still doesn't work - it's not finding the #13#10 in the strings.

Thanks
   Shawn
0
 

Author Comment

by:aztec
ID: 7033354
helloooo, anyone there?
0
 
LVL 1

Expert Comment

by:czechmate
ID: 7034213
The suggestion by MBo is correct, I am using the same
technique and it works.  Perhaps the string, you are
testing this algo with, does not contain any line breaks.  
Text may be wrapped around in a paragraph without line
brakes.

Try it with a memo, type a few lines and terminate each
line with carriage return. Then run your linebreak search  
on Memo.Text.  Or if all fails do something stupid like
this s:= 'asasdadd'#13#13 + 'qweqweqwe' then check s.

cj
0
 

Author Comment

by:aztec
ID: 7034489
MBo/Czechmate -
  I have created some small test data files:

new10.txt - a few email addresses each separated by only a line feed (#10)
new13.txt - a few email addresses each separated by only a carriage return (#13).
new1310.txt - a few email addresses each separated by a carriage return/line feed (#13#10)

...I use MBo's suggestion but I cannot detect the cr/lf in file new1310.txt. Pos returns 0.
 Pos does successfully detect only #10 in new10.txt. But oddly enough it doesn't detect #13 in new13.txt. very strange.

Here is the zip file containing these 3 files:

http://users.hfx.eastlink.ca/~sh/data1310.zip

...can you try your test on these files? I am stumped!

Thanks
   Shawn
0
 
LVL 1

Expert Comment

by:czechmate
ID: 7034820
Shawn,
I tested your files and everything works fine.  Well
there is a hitch: I load your files into a Memo and it
looks like the Memo normalizes line breaks, i.e. #13#10
is always present irrespective which one of your files I
load.

But I can replace #13#10, #13 only or #10 only without problem.  The code is not efficient, but should suffice
to demostrate pos function.

Here's the unit I used, if you want whole project I can
email it to you if you let me have your address.

Regards
cj
=========================================================


unit EOL;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
   if OpenDialog1.Execute then
   begin
      Memo1.Clear;
      Memo1.Lines.LoadFromFile(OpenDialog1.Filename);
   end;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
var   s:string;
      p:integer;
begin
   s:=Memo1.Text;
   repeat
      p:=pos(#13#10, s);
      if p > 0 then
      begin
         delete(s,p,2);
         insert('...',s,p);
      end
      else
      begin
         Memo1.Text:=s;
         exit;
      end;
   until false;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
var   s:string;
      p:integer;
begin
   s:=Memo1.Text;
   repeat
      p:=pos(#13, s);
      if p > 0 then
      begin
         delete(s,p,1);
         insert('...',s,p);
      end
      else
      begin
         Memo1.Text:=s;
         exit;
      end;
   until false;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
var   s:string;
      p:integer;
begin
   s:=Memo1.Text;
   repeat
      p:=pos(#10, s);
      if p > 0 then
      begin
         delete(s,p,1);
         insert('...',s,p);
      end
      else
      begin
         Memo1.Text:=s;
         exit;
      end;
   until false;
end;

end.
0
 

Author Comment

by:aztec
ID: 7035537
czechmate - just try reading the files directly using READLN statement, instead of using MEMO. I do not want to use MEMO in my project. I think you will find that you get my same results.... very strange.
0
 
LVL 1

Accepted Solution

by:
czechmate earned 50 total points
ID: 7035699
Aztec,
I thing you just explained it.  I stand corrected but,
READLN reads strings (lines) up to but excluding CR or
CR/LF sequence.  In other words text read with READLN
never contains CR or CR/LF, that's why you cannot find
it.  It would appear that LF on its own is accepted as
a normal character by READLN and as you say you find
it OK.

I may be wrong here I am typing this stuff as I seem to
remeber it but... :))

Cheers
cj


0
 

Author Comment

by:aztec
ID: 7036525
ahhhh, yes that would explain it then!

I was thinking that READLN include the final #13#10...
0

Featured Post

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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

718 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