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
Solved

finding CR/LF in a text string

Posted on 2002-05-22
14
1,204 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
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
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.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 106
DBGrid or StringGrid ? 6 104
scroll down TListBox component in Delphi 1 19
Microsoft Access 97 and Delphi XE2 9 54
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

861 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