PDA

View Full Version : How to manipulate a string


Jyoti
05-15-2003, 07:00 PM
I have string of characters say (length of the string is 500 ). I need to break the string in chunks of 200 characters each separated by a newline.

So it will write first 200 characters , the next 200 characters in a new line and next 100 characters in a next line and so on till the end of the string is reached.

I have written a code to do this.

Now what I need is that if the last word in a 200 charcter string, should not break and written on a new line. It should complete the word and look for a whitespace and move rest from from the whitespace to the new line and so on.

How can I achieve this.

Here is my code that writes every 200 characters from a string and separates by a newline.

var tComments = "";
var textlen = document.forms[0].Comments.value.length();
final int len = 200;
int start,
int end;

for ( start = 0, end = len; end <= textLen; start += len, end += len )
{

tComments = document.forms[0].Comments.value.substring(start, end);
}


}

if ( start < textLen ) {
tComments = document.forms[0].Comments.value.substring(start);
}

Now what should i do to so that I do not break the last word from the string.

liorean
05-15-2003, 07:31 PM
str=str.replace(/\n$/,'') is the easiest way of doing it.