4

I have a requirement to write strings to text files, but before that I need to ensure that the string does not contain any linefeed, carriage return, or tab characters.

I have tried this code for testing.

<cfset str = "a#chr(10)#bc#chr(13)#def#chr(9)#fg">
<cfset cleanedStr = reReplace(str,"#chr(13)##chr(10)#","","ALL")>
<cfset cleanedStr = reReplace(str,"#chr(10)#","","ALL")>
<cfset cleanedStr = reReplace(str,"#chr(13)#","","ALL")>
<cfset cleanedStr = reReplace(str,"#chr(9)#","","ALL")>

<cffile action="write" file="D:/projects/test.txt" output="#cleanedStr#">

But when I open the file in Eclipse, it still shows the linefeed and cariage return characters.

Text Editor image

So what's the correct way to remove these characters?

Fish Below the Ice
  • 1,273
  • 13
  • 23
Deepak Kumar Padhy
  • 4,128
  • 6
  • 43
  • 79
  • 2
    This [post](http://stackoverflow.com/questions/12016282/how-to-remove-more-then-one-whitespace-from-html) might help you. use `\s` to match for new line, tab, carriage return and spaces as well. – Braj Jul 31 '14 at 14:58
  • 2
    Your test case was flawed. The string you wrote to your text file was `a#chr(10)#bc#chr(13)#deffg` for you keeped replacing the same variable with a new string based on the original. – Twillen Jul 31 '14 at 15:42

1 Answers1

3

Try:

<cfset str = "a#chr(10)#bc#chr(13)#def#chr(9)#fg">
<cfset cleanedStr = reReplace(str,"\s","","ALL")>
<cffile action="write" file="D:/project/test.txt" output="#cleanedStr#">