Replace CR LF with LF in String?

Hello,

have a simple but annoying problem here. For the creation of a .csv file for Excel import I want to replace all CR LF with LF only in the strings, i. e., from "... 0D 0A ..." make "... 0A ..." (hex view) in a string. But DXL does not let me do that (I think). Every time I end up with CR LF...

This is what I currently have


bool replaceNewlines(string s, string &s2) 
{ s2 = 
"" 

int i bool ret = 

false 

for(i = 0; i < length(s); i++) 
{ 

char si = s[i] 

if(si == 
'\n') 
{ si = charOf(10) ret = 

true 
} s2 = s2 si 
"" 
} 

return ret 
}

Any idea?

Thx, hgr00
hgr00 - Thu Jun 09 09:39:30 EDT 2011

Re: Replace CR LF with LF in String?
llandale - Thu Jun 09 16:08:41 EDT 2011

First off, for reasons that escape me and hopefully one of the real clever folks here will explain, I have had significant difficulty using a function output parameter (s2) inside the function. You can set it, but using it (reading it) is unreliable. So you need to accumulate your final string, perhaps variable s3, and at the bottom set s2=s3 before returning.

Second, the character '\n' is indeed already ascii decimal 10. So your function right now replaces 10 with 10.

Thirdly you want to search for characters 13 and 10 adjacent, and remove 13.

Fourth, this is screaming for use of a Buffer. <s2 = s2 si ""> inside a loop will exponentially eat up memory. Better to just insert "si" into the end of the Buffer.

Fifth, I've exported quite a lot and have never run into this issue. I wonder where you are getting your original data.

Try the following:

bool replaceNewlines(string s, string &s2) 
{ Buffer buf = create() 

int i bool ret = 

false 

char si, sj 

for(i = 0; i < length(s)-1; i++) 
{ si = s[i] sj = s[i+1] 

if(intOf(si) == 13  and intOf(sj) == 10) 
{ ret = 

true 
} 

else buf += si 
} s2 = stringOf(buf) delete(buf) 

return ret 
}   

void PrintS(string Label, s2) 
{  

int i 

char c print Label 
"\n" 

for (i=0; i<length(s2); i++) 
{  c=s2[i] print c 
"\t" (intOf(c)) 
"\n" 
} 
}   

void DoTest(string s) 
{   string s2 replaceNewlines(s, s2) print 
"[" s 
"]\t[" s2 
"\n" PrintS(
"s: ", s) PrintS(
"s2:", s2) 
}   DoTest(
"ABC" charOf(13) charOf(10) 
"DEF")

  • Louie

Re: Replace CR LF with LF in String?
llandale - Thu Jun 09 16:29:45 EDT 2011

My ancient notes show the following ascii codes of interest:

. 0        

null 8        backspace?     
'\b' 9   TAB  tab            
'\t' 10   EOL  end of line    
'\n' also 
'newline' 11   LF   word line feed 
'\v' 12   FF   form feed      
'\f' (word page 

break) 13   CR   cariage 

return  
'\r' 26        end of file?

I've never successfully deployed the backspace to actually perform a backspace.

Seems to me char(10) acts like a form-feed (go down one line) and also a goto-start-of-line in most Windows applications, whereas in other environments char(10) simply goes to the start of the current line, forcing the need for a char(13) to get to the next line. char(13) doesn't seem to do anything in DXL.

  • Louie

Re: Replace CR LF with LF in String?
llandale - Thu Jun 09 16:37:28 EDT 2011

llandale - Thu Jun 09 16:08:41 EDT 2011
First off, for reasons that escape me and hopefully one of the real clever folks here will explain, I have had significant difficulty using a function output parameter (s2) inside the function. You can set it, but using it (reading it) is unreliable. So you need to accumulate your final string, perhaps variable s3, and at the bottom set s2=s3 before returning.

Second, the character '\n' is indeed already ascii decimal 10. So your function right now replaces 10 with 10.

Thirdly you want to search for characters 13 and 10 adjacent, and remove 13.

Fourth, this is screaming for use of a Buffer. <s2 = s2 si ""> inside a loop will exponentially eat up memory. Better to just insert "si" into the end of the Buffer.

Fifth, I've exported quite a lot and have never run into this issue. I wonder where you are getting your original data.

Try the following:


bool replaceNewlines(string s, string &s2) 
{ Buffer buf = create() 

int i bool ret = 

false 

char si, sj 

for(i = 0; i < length(s)-1; i++) 
{ si = s[i] sj = s[i+1] 

if(intOf(si) == 13  and intOf(sj) == 10) 
{ ret = 

true 
} 

else buf += si 
} s2 = stringOf(buf) delete(buf) 

return ret 
}   

void PrintS(string Label, s2) 
{  

int i 

char c print Label 
"\n" 

for (i=0; i<length(s2); i++) 
{  c=s2[i] print c 
"\t" (intOf(c)) 
"\n" 
} 
}   

void DoTest(string s) 
{   string s2 replaceNewlines(s, s2) print 
"[" s 
"]\t[" s2 
"\n" PrintS(
"s: ", s) PrintS(
"s2:", s2) 
}   DoTest(
"ABC" charOf(13) charOf(10) 
"DEF")

  • Louie

Oops:

bool replaceNewlines(string s, string &s2) 
{ Buffer buf = create() 

int i bool ret = 

false 

char si, sj 

for(i = 0; i < length(s); i++) 
{ si = s[i] sj = s[i+1] 

if (i+1 < length(s) and intOf(si) == 13  and intOf(sj) == 10) 
{ ret = 

true 
} 

else buf += si 
} buf += charOf(0) s2 = stringOf(buf) delete(buf) 

return ret 
}   

void PrintS(string Label, s2) 
{  

int i 

char c print Label 
"\n" 

for (i=0; i<length(s2); i++) 
{  c=s2[i] print 
".." c 
"\t" (intOf(c)) 
"\n" 
} 
}   

void DoTest(string s) 
{   string s2 replaceNewlines(s, s2) print 
"[" s 
"]\t[" s2 
"]\n" PrintS(
"s: ",  s) PrintS(
"s2:", s2) 
}   DoTest(
"ABC" charOf(13) charOf(10) charOf(12) 
"DEF")