SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Angrypoonani's Avatar
    Join Date
    Mar 2011
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Line breaks while building a string in a for loop

    I have a piece of code that looks like this
    Code:
    for (var k=0; k<myArray.length; k++)
    	{
    		minuslen = myArray.length-1;
    		myString += myArray[k];
    		if (k<minuslen) {
    		myString += " : "+myArray[k+1];
    		}
    		myString += "\n";
    		k++;
    	}
    myArray = myString.split(" : ");
    The output will contain a literal \n instead of a line break which is not really the desired output. What would be a way to insert the line breaks to get the desired output?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    A literal \n in JavaScript is written "\\n" - the \n that you have is a linebreak character and not a literal \n.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast Angrypoonani's Avatar
    Join Date
    Mar 2011
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    A literal \n in JavaScript is written "\\n" - the \n that you have is a linebreak character and not a literal \n.
    Thank you your response felgall.
    Yes, I realize the \n is a linebreak character and that is the desired character. What I'm getting at and what I don't understand is why it doesn't create a linebreak in the output of the final string and instead adds a literal \n.
    is it because for the linebreak character to be interpreted I have to call a print/write-to-screen function?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Angrypoonani View Post
    is it because for the linebreak character to be interpreted I have to call a print/write-to-screen function?
    Yes. In JavaScript a linebreak displays as \n - to get it to actually display as a linebreak you need to view it from outside JavaScript - eg. by writing it into a web page and then viewing the updated source of the page via the browser debugging tools.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •