Solved

finding CR/LF in a text string

Posted on 2002-05-22
14
1,184 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
14 Comments
 
LVL 1

Expert Comment

by:thegroup
Comment Utility
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
Comment Utility
CRLFPos:=pos(#13#10,YourString);
0
 

Author Comment

by:aztec
Comment Utility
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
 

Author Comment

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

Author Comment

by:aztec
Comment Utility
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
Comment Utility
sorry, false alarm, I had the arguments in the Pos statement reversed. Duhhhh.....
0
 

Author Comment

by:aztec
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:aztec
Comment Utility
helloooo, anyone there?
0
 
LVL 1

Expert Comment

by:czechmate
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ahhhh, yes that would explain it then!

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

9 Experts available now in Live!

Get 1:1 Help Now