Copy link to clipboard
Copied
Hi,
I have a java object that returns a string object from a method in this format:
This is a test \n And it continues \n And continues \n etc...
When I get this string object in ColdFusion, somehow it doesnt recognize \n. I would like to replace \n with <br> html tag. Does anyone know know how to solve to issue?
Btw, I tried using ReReplace, and its not working for me.
Thanks,
1 Correct answer
First, did you determine if it really is chr(10)? No point trying to replace something unless you know what that "something" is
I tried using something along this line:
ReReplace(description, "chr(10)", "<br>")
If it is really chr(10), get rid of the double quotes around it. Otherwise CF will be searching for the literal characters "chr(10)", not a line feed.
Copy link to clipboard
Copied
Does anyone know know how to solve to issue?
You need to determine how the character(s) are perceived in CF. IIRC, "\n" is usually chr(10). But verify that with string functions. Just find the offending character(s) and check the ascii value(s).
Copy link to clipboard
Copied
How do I find the "offending character(s)"? I assume using asc() function and then replacing it with replace() function?
I also, I should have noted:
When I write output to the browser, I the result as (without escape characters):
This is a test And it continues And continues etc...
Copy link to clipboard
Copied
#replace(stringReturnVar, "\n", "<br />", "all")#
You don't need reReplace() in this case, just simple string replace
Copy link to clipboard
Copied
You don't need reReplace() in this case, just simple string
replace
Only if it is the literal characters "\n". Usually "\n" is equivalent to chr(10) in java.
Copy link to clipboard
Copied
cfSearching,
I tried using something along this line:
ReReplace(description, "chr(10)", "<br>")
But this does not seems to work. I have tried this as well:
ReReplace(description, "\n", "<br>")
If I dump the variable, CF shows everything on one line without "\n" in the expression.
Copy link to clipboard
Copied
First, did you determine if it really is chr(10)? No point trying to replace something unless you know what that "something" is
I tried using something along this line:
ReReplace(description, "chr(10)", "<br>")
If it is really chr(10), get rid of the double quotes around it. Otherwise CF will be searching for the literal characters "chr(10)", not a line feed.
Copy link to clipboard
Copied
Yup, it was chr(10).
I verfied it using mid() along with asc(). After it printed out asii value for each character, I noticed that ch(10) in each return.
Also, thanks for pointing out not use reReplace()
Copy link to clipboard
Copied
So it is working now with replace() and chr(10)?
Also, thanks for pointing out not use reReplace()
Nothing technically wrong it. In fact I am a little surprised that did not work with "\n".
Copy link to clipboard
Copied
ReReplace(description, "\n", "
") won't work, because that's trying to
replace "n", since "\" is the escape character in regex.
Just try this:
Replace(description, "\n", "
", "all") <== note that this is NOT
REReplace, just Replace
If the newline character is actually the hard return, then try this:
Replace(description, chr(10), "
", "all")
Again, with REReplace you were trying a regex on something that's not a
regular expression, it's just a string. Also, with quotes around the
chr(10), it replaces the actual string "chr(10)", which won't exist in the
first place. The example above will properly find end of line and replace
with "
". If you're not sure how it's actually coded, you could nest
both:
#Replace(Replace(description, "\n", "
", "all"), chr(10), "
",
"all")#
Copy link to clipboard
Copied
Whilst "\" is the escape character, "\n" is the "new line character".
Read the docs:
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html#WSc3ff6d0ea77859461172e0811cbec22c24-7e92
\n
Newline character
--
Adam
Copy link to clipboard
Copied
I think I accidently pressed the Post Message button last time.
"Also, thanks for pointing out to use char(10) without quotes"
JMF,
I meant to say that not to use "" in the char(10). Because, it was assuming that I want to replace the string of char(10) rather then a new line character.
The new line character is hard written when it returned the statement.
Thanks,
Copy link to clipboard
Copied
JMF,
I went ahead and tested both functions, the results came out exactly the same.
#ReReplace(description, chr(10), '<br />', "all")#
#Replace(description, chr(10), "<br/>", "all")#
The results shows as I wanted, which is good.
I was thinking the reReplace is for regex expression replacements.
Copy link to clipboard
Copied
splitzer wrote:
I was thinking the reReplace is for regex expression replacements.
It is, it just happens that the regular experssion in your example:
#ReReplace(description, chr(10), '<br />', "all")#
Is a single new line character. But that is a perfectly valid regular expression

