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

how replace comma with break line

Guest
Jan 07, 2014 Jan 07, 2014

Hi, 

Instead of John,Dee, i want it to show

John

Dee.

my code isn't do what i want, can anyone help pls?

thanks

<cfset mystring ="John,Dee" />

<cfset TheText = replace(mystring, ',', "#chr(10)##chr(13)#", "ALL") />

<cfoutput>

#TheText#

</cfoutput>

2.6K
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
Enthusiast ,
Jan 07, 2014 Jan 07, 2014

Your code should work fine.

Problem is if you're outputting to a browser, it will attempt to render it as HTML (unless you send a <cfcontent type="text/plain" />)  As such, if you run your code, you'll see both John and Dee on the same line.  But do a "VIEW SOURCE" in your browser, and you'll see they're being output on new lines (because of your chr(10) and chr(13)).

This is the default behavior of a browser and the text/html mime type.

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
Adobe Employee ,
Jan 07, 2014 Jan 07, 2014

In addition to Aegis's point, a <br> between #chr(10)##chr(13)# will give you the desired result. But that is only appropriate, if you are viewing the result on a web browser.

Regards,

Anit Kumar

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
Enthusiast ,
Jan 07, 2014 Jan 07, 2014

Yeah.  If you're gonna go ACTUAL HTML output, then forget the line break and just put each indexed value into a <p> tag (which, because it's a block-level element, will place it on each line)

<cfset myList = 'Aegis,Kleais' />

<cfoutput>

<cfloop list="#myList#" index="i">

     <p>#i#</p> 

</cfloop>

</cfoutput>

This should output:

Aegis

Kleais

The spacing between the text can be controlled via CSS.

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
New Here ,
Jan 08, 2014 Jan 08, 2014

<cfoutput>#listchangedelims('to,from','<br>',',')#</cfoutput>

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
Enthusiast ,
Jan 08, 2014 Jan 08, 2014
LATEST

Any list function that specifies the default delimiter (a comma) for its arguments, is an optional argument, and as such, will not need to be provided.  For example, you could use:

<cfoutput>#listChangeDelims( 'to,from', '<br>' )#</cfoutput>

And the above will assume the list is comma-delimited (which it is), and replace it with <br>, however, this too is under the assumption that CF is being used to generate HTML rather than plain text.  Set the mime type accordingly.

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