How to replace CR and/or LF in a string

 

Hi Folks,

I have a string which I am saving to a file. Problem is, that the string has 4 CR in it, according to notepad++, which I want to replace and take out the line breaks.

  • CR = Carriage Return ( \r ,  0x0D  in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.
  • LF = Line Feed ( \n ,  0x0A  in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.


I tried the following to replace the CR with e.g. pipe:


StringReplace(comment,'\x10',"|");

StringReplace(comment,'\r',"|"); 

like described here: https://docs.mql4.com/basis/types/integer/symbolconstants

But nothing works. Does anybody have an idea?

P.S. I am actually using mql5

P.P.S the file is attached.

Regards

Thomas

Character Constants - Integer Types - Data Types - Language Basics - MQL4 Reference
Character Constants - Integer Types - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Character Constants - Integer Types - Data Types - Language Basics - MQL4 Reference
Files:
signals.csv  1 kb
 
  1. If you are using MQL5 then please don't post in the MQL4 section.
  2. If using MQL5 then please reference the MQL5 documentation and not the MQL4 documentation (they can be very different).
  3. Just showing us a function implementation is not sufficient for us to determine what you are doing incorrectly.
    Show a code sample that can be compiled and tested and demonstrates the issue you are experiencing.
    Provide the source sample text that you are processing and not only the result.

MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
  • www.mql5.com
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
 

Also, please note that the FileWrite function automatically adds a "\r\n" to the end.

If you want to write text to a file without the automatic "\r\n", then use FileWriteString.

FileWrite

Writes data to a file of CSV or TXT type

FileWriteString

Writes the value of a string parameter into a BIN or TXT file starting from the current position of the file pointer

 

Just noticed one more thing ...

StringReplace(comment,'\x10',"|");

StringReplace(comment,'\r',"|"); 

It should be ...

StringReplace( comment, "\xA", "|" ); // or ...
StringReplace( comment, "\10", "|" ); // or ...
StringReplace( comment, "\n",  "|" ); 

StringReplace( comment, "\r",  "|" ); // use " and not ' as they are string parameters and not character parameters.

You can also combine them ...

StringReplace( comment, "\r\n", "|" );
 

Dear Fernando,

thanks for the hints, I will respect them next time.

At the end your last comment did the trick:

StringReplace(comment,"\13","|"); //13 = CR , LF = 10 


Obrigado!

Reason: