[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1292
  • Last Modified:

finding CR/LF in a text string

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
aztec
Asked:
aztec
1 Solution
 
thegroupCommented:
Pos (String,#13), if this return any value >= 0 the character #13 (cr) exists. The same for (lf = #10)
0
 
MBoCommented:
CRLFPos:=pos(#13#10,YourString);
0
 
aztecAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
aztecAuthor Commented:
sorry, false alarm, I had the arguments in the Pos statement reversed. Duhhhh.....
0
 
aztecAuthor Commented:
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
 
aztecAuthor Commented:
sorry, false alarm, I had the arguments in the Pos statement reversed. Duhhhh.....
0
 
aztecAuthor Commented:
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
 
aztecAuthor Commented:
helloooo, anyone there?
0
 
czechmateCommented:
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
 
aztecAuthor Commented:
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
 
czechmateCommented:
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
 
aztecAuthor Commented:
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
 
czechmateCommented:
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
 
aztecAuthor Commented:
ahhhh, yes that would explain it then!

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now