Copiar vínculo al Portapapeles
Copiado
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,
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.
Copiar vínculo al Portapapeles
Copiado
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).
Copiar vínculo al Portapapeles
Copiado
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...
Copiar vínculo al Portapapeles
Copiado
#replace(stringReturnVar, "\n", "<br />", "all")#
You don't need reReplace() in this case, just simple string replace ![]()
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
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()
Copiar vínculo al Portapapeles
Copiado
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".
Copiar vínculo al Portapapeles
Copiado
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")#
Copiar vínculo al Portapapeles
Copiado
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
Copiar vínculo al Portapapeles
Copiado
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,
Copiar vínculo al Portapapeles
Copiado
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.
Copiar vínculo al Portapapeles
Copiado
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
¡Prepárate! En enero llega una experiencia mejorada de Adobe Community.
Más información