Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to remove line feed from ascii string

I have a program where an ascii string is received from the serial port and put into an sbuf (serial buffer) . When I display the string in a TListBox the line feed shows up as a small black vertical line. I have include a code snippet where I receive the string into the serial buffer. I was wondering if anyone can tell me a good way to remove the line feed before going to the TListBox. Also note that the string does not have a carriage return just a line feed. Thank you in advance!
Craig

procedure TForm1.rxProc(const buf; len: word);
var
bx: byte;
idx: integer;
begin
for idx := 0 to len - 1 Do Begin
bx := TByteArray(buf) [idx]; // Get next byte
sbuf := sbuf + CHR(bx); //Append byte to string
if (bx = 10) then begin // Byte is LF
SendData (sbuf); //Send serial buffer to map server
lb.Items.Insert(0, sbuf); //Insert sbuf at top of listbox
sbuf :=''; // Clear sbuf
end;
end;
end;

Comments

  • zibadianzibadian Posts: 6,349Member
    : I have a program where an ascii string is received from the serial
    : port and put into an sbuf (serial buffer) . When I display the
    : string in a TListBox the line feed shows up as a small black
    : vertical line. I have include a code snippet where I receive the
    : string into the serial buffer. I was wondering if anyone can tell me
    : a good way to remove the line feed before going to the TListBox.
    : Also note that the string does not have a carriage return just a
    : line feed. Thank you in advance!
    : Craig
    :
    : procedure TForm1.rxProc(const buf; len: word);
    : var
    : bx: byte;
    : idx: integer;
    : begin
    : for idx := 0 to len - 1 Do Begin
    : bx := TByteArray(buf) [idx]; // Get next byte
    : sbuf := sbuf + CHR(bx); //Append byte to string
    : if (bx = 10) then begin // Byte is LF
    : SendData (sbuf); //Send serial buffer to map server
    : lb.Items.Insert(0, sbuf); //Insert sbuf at top of listbox
    : sbuf :=''; // Clear sbuf
    : end;
    : end;
    : end;
    The easiest is to use the Replace() function. This allows your to replace any substring (or single character) with something else (or nothing). See help files for more info on the Replace() function.
  • RFsignalRFsignal Posts: 9Member
    : : I have a program where an ascii string is received from the serial
    : : port and put into an sbuf (serial buffer) . When I display the
    : : string in a TListBox the line feed shows up as a small black
    : : vertical line. I have include a code snippet where I receive the
    : : string into the serial buffer. I was wondering if anyone can tell me
    : : a good way to remove the line feed before going to the TListBox.
    : : Also note that the string does not have a carriage return just a
    : : line feed. Thank you in advance!
    : : Craig
    : :
    : : procedure TForm1.rxProc(const buf; len: word);
    : : var
    : : bx: byte;
    : : idx: integer;
    : : begin
    : : for idx := 0 to len - 1 Do Begin
    : : bx := TByteArray(buf) [idx]; // Get next byte
    : : sbuf := sbuf + CHR(bx); //Append byte to string
    : : if (bx = 10) then begin // Byte is LF
    : : SendData (sbuf); //Send serial buffer to map server
    : : lb.Items.Insert(0, sbuf); //Insert sbuf at top of listbox
    : : sbuf :=''; // Clear sbuf
    : : end;
    : : end;
    : : end;
    : The easiest is to use the Replace() function. This allows your to
    : replace any substring (or single character) with something else (or
    : nothing). See help files for more info on the Replace() function.

    Thank you very much for the tip, I will give that try.

  • ramsambaramsamba Posts: 1Member
    : : I have a program where an ascii string is received from the serial
    : : port and put into an sbuf (serial buffer) . When I display the
    : : string in a TListBox the line feed shows up as a small black
    : : vertical line. I have include a code snippet where I receive the
    : : string into the serial buffer. I was wondering if anyone can tell me
    : : a good way to remove the line feed before going to the TListBox.
    : : Also note that the string does not have a carriage return just a
    : : line feed. Thank you in advance!
    : : Craig
    : :
    : : procedure TForm1.rxProc(const buf; len: word);
    : : var
    : : bx: byte;
    : : idx: integer;
    : : begin
    : : for idx := 0 to len - 1 Do Begin
    : : bx := TByteArray(buf) [idx]; // Get next byte
    : : sbuf := sbuf + CHR(bx); //Append byte to string
    : : if (bx = 10) then begin // Byte is LF
    : : SendData (sbuf); //Send serial buffer to map server
    : : lb.Items.Insert(0, sbuf); //Insert sbuf at top of listbox
    : : sbuf :=''; // Clear sbuf
    : : end;
    : : end;
    : : end;
    : The easiest is to use the Replace() function. This allows your to
    : replace any substring (or single character) with something else (or
    : nothing). See help files for more info on the Replace() function.

    The Replace() function is incorrect--it's actually StringReplace(). And this will also not remove the control characters. If the control characters are at the beginning or the end of the string, then use Trim().
    I had a PHP echo function returning #$A'mystring' and that's where Trim() did the job
  • zibadianzibadian Posts: 6,349Member
    : : : I have a program where an ascii string is received from the serial
    : : : port and put into an sbuf (serial buffer) . When I display the
    : : : string in a TListBox the line feed shows up as a small black
    : : : vertical line. I have include a code snippet where I receive the
    : : : string into the serial buffer. I was wondering if anyone can tell me
    : : : a good way to remove the line feed before going to the TListBox.
    : : : Also note that the string does not have a carriage return just a
    : : : line feed. Thank you in advance!
    : : : Craig
    : : :
    : : : procedure TForm1.rxProc(const buf; len: word);
    : : : var
    : : : bx: byte;
    : : : idx: integer;
    : : : begin
    : : : for idx := 0 to len - 1 Do Begin
    : : : bx := TByteArray(buf) [idx]; // Get next byte
    : : : sbuf := sbuf + CHR(bx); //Append byte to string
    : : : if (bx = 10) then begin // Byte is LF
    : : : SendData (sbuf); //Send serial buffer to map server
    : : : lb.Items.Insert(0, sbuf); //Insert sbuf at top of listbox
    : : : sbuf :=''; // Clear sbuf
    : : : end;
    : : : end;
    : : : end;
    : : The easiest is to use the Replace() function. This allows your to
    : : replace any substring (or single character) with something else (or
    : : nothing). See help files for more info on the Replace() function.
    :
    : The Replace() function is incorrect--it's actually StringReplace().
    : And this will also not remove the control characters. If the control
    : characters are at the beginning or the end of the string, then use
    : Trim().
    : I had a PHP echo function returning #$A'mystring' and that's where
    : Trim() did the job
    :
    You're parely correct: the function is called StringReplace(). But it can replace any sequence of characters with any other sequence of characters. Thus
    [code]
    StringReplace(SomeText, #13#10, '', [rfReplaceAll]);
    [/code]
    replaces all the line breaks.
Sign In or Register to comment.