...

View Full Version : Splitting a Text String Question



jason_kelly
09-23-2011, 08:58 PM
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

blaze4218
09-23-2011, 09:00 PM
If that's indeed javascript, and you are inserting it into HTML, you would want



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

xelawho
09-23-2011, 09:20 PM
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

Raphael
09-23-2011, 09:22 PM
If you want spaces between each line you could use <p> tags or <div>'s, etc. instead of <br>'s.

blaze4218
09-23-2011, 09:27 PM
@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"

xelawho
09-23-2011, 09:37 PM
oh :confused:

I thought we were talking about this...



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

blaze4218
09-23-2011, 09:43 PM
Yeah... I think some clarification is needed on both the question, and especially the usage... @jason_kelly got any code to post?

jason_kelly
09-23-2011, 10:35 PM
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

blaze4218
09-23-2011, 10:40 PM
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)

xelawho
09-23-2011, 10:43 PM
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)

DaveyErwin
09-24-2011, 12:35 AM
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


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

jason_kelly
09-24-2011, 03:24 PM
Thanks again for everyones help. Blaze and Davey's examples combined took the cake here :)



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum