Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Wrap Function

Status
Not open for further replies.

dougcranston

Technical User
Oct 5, 2001
326
US
I have a need to be able to format long strings for inclusion in an email message. I have one that works but it is VBScript. What I want is a Javascript function.

I am looking for a function that can take an string of text and based on the line length, insert \n at the nearest "blank" space.

Searching the web, I have found a couple of Javascripts that will break up text, but it does so at the exact line length resulting in words being split. Looks ugly. Example of one is found at:
Anyone have any suggestions?

Thanks in advance.

DougC
 
Here is one (made spontaneously)
Code:
function lineBreaks(text, maxlength)
{
  var len = text.length;
  var pos = -1;
  for (var i=maxlength; i<len; i += maxlength)
  {
    for (var pos = i; text.charAt(pos) != " "; pos++)
    {
      if (pos == i-maxlength)
      {
        pos = i;
        break;
      }
    }
    text = text.substring(0,pos)+"\n"+text.substring(pos,len-1);
  }
  return text;
}

This probably requires some editing.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Try this:

<script language="javascript">

var maxlength=20;
function splitstring(onestring)
{
var lastslash = onestring.lastIndexOf('\n');
do
{
var newslash = lastslash + maxlength;
while (onestring.charAt(newslash) != ' ' && newslash > -1)
{
newslash--;
}
if (newslash > -1)
{
onestring=onestring.substr(0, newslash) + '\n' + onestring.substr(newslash + 1);
}
lastslash = onestring.lastIndexOf('\n');
}
while (onestring.length - lastslash > maxlength);
return onestring;
}

</script>
 
You can set the maxlength to whatever you want, or pass the parameter to the function like chessbot did in his example.

Lee
 
Here is a version that actually works:
Code:
function lineBreaks(text, maxlength)
{
  var len = text.length;
  var pos = -1;
  var replace = true;
  for (var i=maxlength; i<len; i += maxlength)
  {
    var separator = "\n"
    for (var pos = i; text.charAt(pos) != " "; pos--)
    {
      if (pos == i-maxlength)
      {
        pos = i;
        separator += text.charAt(pos);
        len++;
        break;
      }
    }
    text = text.substring(0, pos) + separator + text.substring(pos+1, len-1);
    i = pos;
    replace = true;
  }
  return text;
}

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
trollacious and chessbot,

Outstanding.. Just got to my desk and found your responses.

Both look great. Will be trying them out shortly.

On quick glance they both will do the job..

Again.. Thank you.. I do appreciate the quick response.

Dougc :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top