Forum Moderators: open

Message Too Old, No Replies

Remove / delete last line from textarea

remove delete last line text file textarea

         

total n00ber

8:13 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



Hi, I have a script that reads a multi-line textfile and displays the contents into a textarea. I do this with a while loop:

while(true) { 
contents += fso.readline() + "\n";
}

The problem is that each time I read the file, it adds an empty line to the textarea since it has to generate the linefeed ("\n") on the readline() function; this goes for the last line in the read.

So now I want to write a function to remove the last (blank) line before I save the file and I don't know how to find the blank lines. Here's what I have so far for the function:

function removeBlankLine() { 
var str = document.form1.myTextArea.value;
// need a loop of some sort to find the last line?
str = str.replace(/\n/g, ""); // instead of the quotes, trim length; one backspace?
obj.value = str;
}

Fotiman

9:14 pm on Dec 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What if you just do this instead:


var contents = null;
while(true) {
if( contents ) contents += "\n";
contents += fso.readline();
}

So, assuming contents is null, the first time through this will not add the newline, but will add the fso.readline() results. The next time, the "if( contents )" check will evaluate to true, and will add a newline, then add the fso.readline() results. That way, there will be no extra \n at the end of contents.

Alternatively, you could do this:


while(true) {
contents += fso.readline() + "\n";
}
contents = contents.substring(0,contents.length-1);

[edited by: Fotiman at 9:18 pm (utc) on Dec. 28, 2006]

total n00ber

10:23 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



contents = contents.substring(0,contents.length-1);

Okay, that works great. I just put that line in before my save function and left the read function the same. Thank you!