Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Replace New Line Expression

Explorer ,
Nov 02, 2010 Nov 02, 2010

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,

5.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Nov 03, 2010 Nov 03, 2010

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.

Translate
Valorous Hero ,
Nov 02, 2010 Nov 02, 2010

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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 02, 2010 Nov 02, 2010

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 03, 2010 Nov 03, 2010

#replace(stringReturnVar, "\n", "<br />", "all")#

You don't need reReplace() in this case, just simple string replace

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 03, 2010 Nov 03, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2010 Nov 03, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 03, 2010 Nov 03, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2010 Nov 03, 2010

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()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 04, 2010 Nov 04, 2010

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".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 04, 2010 Nov 04, 2010

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")#

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 04, 2010 Nov 04, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 04, 2010 Nov 04, 2010

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,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 04, 2010 Nov 04, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 04, 2010 Nov 04, 2010
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources