Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    178
    Thanks
    138
    Thanked 0 Times in 0 Posts

    Splitting a Text String Question

    Hello,

    How would I seperate a text string such that it would appear on seperate lines ie.

    Initial Input:
    -------------------------------------------

    StrMsg = "This is an example of a string that will appear on seperate lines"
    "Hoping that this fully works, there will be no errors and all will be well"
    "Thank you to the experts and pros on this forum who are here to help"
    "This is the last line of text."

    Expected Outcome:
    -------------------------------------------

    This is an example of a string that will appear on seperate lines

    Hoping that this fully works, there will be no errors and all will be well

    Thank you to the experts and pros on this forum who are here to help

    This is the last line of text.




    Much thanks and appreciation for all your help

    J

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    If that's indeed javascript, and you are inserting it into HTML, you would want

    Code:
    StrMsg = "<br />This is an example of a string that will appear on seperate lines"
    +"<br />Hoping that this fully works, there will be no errors and all will be well"
    +"<br />Thank you to the experts and pros on this forum who are here to help"
    +"<br />This is the last line of text."

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    yes, it's a little hard to say because the example given doesn't look (to me, anyway) like a valid string... if you come up with a real life example it will make it much simpler

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    If you want spaces between each line you could use <p> tags or <div>'s, etc. instead of <br>'s.

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    @Raphael very true, however, that would create a structural separation that would make navigating the content via the DOM unnecessarily convoluted... If all he is looking for is a line-break, "<br />" would propably be best. but if it goes with his other post about vb email, he might need the "\n"

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    oh

    I thought we were talking about this...

    Code:
    <div id="myDiv"></div>
    <script type="text/javascript">
    StrMsg = '"This is an example of a string that will appear on seperate lines""Hoping that this fully works, there will be no errors and all will be well""Thank you to the experts and pros on this forum who are here to help""This is the last line of text."'
    newStr=StrMsg.replace(/"/g,'<br>')
    document.getElementById('myDiv').innerHTML=newStr;
    </script>

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Yeah... I think some clarification is needed on both the question, and especially the usage... @jason_kelly got any code to post?

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    178
    Thanks
    138
    Thanked 0 Times in 0 Posts
    Thanks very much for everyones help.

    Basically, this type of text is going into a plain text email.

    However, as I have it right now the text just looks like a run on sentence.

    I'd like to test this, by using an alert box:

    with the outcome:

    Line1

    Line2

    Line3

    Line4

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Code:
    StrMsg = "<br />This is an example of a string that will appear on seperate lines"
    +"<br />Hoping that this fully works, there will be no errors and all will be well"
    +"<br />Thank you to the experts and pros on this forum who are here to help"
    +"<br />This is the last line of text."
    alert(StrMsg)

  • Users who have thanked blaze4218 for this post:

    jason_kelly (09-24-2011)

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    right.

    but to do it programatically, there has to be some sort of predictable point at which to insert the line break. the code in post #6 worked (although it was a bit of a cheat) because it put in the line breaks wherever it found a "

    but if you just have one big long sentence it will make it harder but not impossible... which is why we ask to see your string (or an example that has the same characteristics)

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by jason_kelly View Post
    Thanks very much for everyones help.

    Basically, this type of text is going into a plain text email.

    However, as I have it right now the text just looks like a run on sentence.

    I'd like to test this, by using an alert box:

    with the outcome:

    Line1

    Line2

    Line3

    Line4
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script type="text/javascript">
    StrMsg = "This is an example of a string that will appear on seperate lines"
    +"\nHoping that this fully works, there will be no errors and all will be well"
    +"\nThank you to the experts and pros on this forum who are here to help"
    +"\nThis is the last line of text."
    alert(StrMsg)
    </script>
    </head>
    <body>
    <div id="container">
    
    </div>
    </body>
    </html>

  • Users who have thanked DaveyErwin for this post:

    jason_kelly (09-24-2011)

  • #12
    Regular Coder
    Join Date
    Sep 2011
    Posts
    178
    Thanks
    138
    Thanked 0 Times in 0 Posts
    Thanks again for everyones help. Blaze and Davey's examples combined took the cake here

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script type="text/javascript">
    StrMsg = "This is an example of a string that will appear on seperate lines"
    +"<br><br>Hoping that this fully works, there will be no errors and all will be well"
    +"<br><br>Thank you to the experts and pros on this forum who are here to help"
    +"<br><br>This is the last line of text."
    
    document.write(StrMsg)
    </script>
    
    </head>
    <body>
    
    </div>
    </body>
    </html>
    Thanks again for all the wonderful help and support from everyone.

    Cheers and have an awesome weekend all.

    J


  •  

    Posting Permissions

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