?
Solved

finding CR/LF in a text string

Posted on 2002-05-22
14
Medium Priority
?
1,260 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
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.

 

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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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