Link to home
Start Free TrialLog in
Avatar of snow_Queen
snow_QueenFlag for Israel

asked on

Delete extra blank line in textarea

How can I delete extra blank lines from the end of  textarea?
Avatar of BasiKobrA
BasiKobrA

This is probably what you need :

Regards.
yourTextArea.setText( yourTextArea.getText().trim() );

Open in new window

Sorry, I found this in the algorithms section and thought it was java related.
Though, the Trim() function call on the string might still be what you need in that case.
"Trim()" removes SPACES, not characters, and there is no such verb in JavaScript.

var ra = document.getElementById('my_textarea').value.split('\n');
var ta = '';

for (var i = 0; i < ra.length; i++) {
    if (ra[i] !== '') {
        ta += ra[i] + '\n';
    }
}

There are other, more sophisticated methods, but this should get you started.
Avatar of snow_Queen

ASKER

I need remove blank lines from the _END_ of textarea! Not all blank lines. Using ASP/VBScript platform.
Then you will have to start at the end of the line, and if the char is a '\n' remove it, and check the new last char, and if it is a '\n', remove it, and so on.
Avatar of Wayne Barron
snow queen
I am not sure how to use this, but maybe it might help.
You can use this to remove the unwanted spaces at the end of a string

Dim sChars As String = " "
YourString.TrimEnd(sChars)

hth
Carrzkiss
sorry.
That was for VB.NET
Maybe you can still use it.

As this:
Dim sChars
sChars As String = " "
YourString.TrimEnd(sChars)
Think this should work...
Do Until right(yourstring,2)<>vbcrlf
  yourstring=left(yourstring, len(yourstring)-2)
Loop

Open in new window

That will delete ALL of the "blank lines", including the first.
A "vbCrLf" is not an indicator of trailing blank lines *unless* they are the last 2 characters of the file. So you will have to check:

pos = Length(your_string) - 2 ' Adjust for at least one trailing "vbCrLf"
while pos > 0
    if InStr(pos, 2) = vbCrLf And pos = Length(your_string) - 2 Then
        your_string = Left(your_string, pos) ' Strip off the "blank line"
    end if
    pos = pos - 2
wend
Let's try that again...

pos = Length(your_string) - 2 ' Adjust for at least one trailing "vbCrLf"
while pos > 1
    if Mid(your_string, pos, 2) = vbCrLf And pos = Length(your_string) - 2 Then
        your_string = Left(your_string, pos) ' Strip off the "blank line"
        pos = pos - 1 ' Account for "Cr" portion
    end if
    pos = pos - 1 ' Account for Lf portion (always backup 1 char)
wend
ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> If Len(Trim(aTemp(i))) > 0 The Exit For

Should of course be (a letter "n" is missing)

If Len(Trim(aTemp(i))) > 0 Then Exit For
Assuming you mean classic asp / vbscript

mytextarea = rtrim(Request.Form("mytextarea")

Open in new window

left off a )  *slap*
mytextarea = rtrim(Request.Form("mytextarea"))

Open in new window

>>Because I consider also a line with just spaces a blank line.

Not me. That is just whitespace. I'm thinking the Asker means extra CR/FL chars. After all, there had to be CR/LF if the line is only spaces, right?
I don't know exactly what the Asker means. But I just imagined that if somebody enters some text in a textarea, you'd want to store the meaningful text only.

As I type in a text area, I often hit <enter> repeatedly and then use the arrow-up the continue where I was typing, which results in a number of trailing line breaks. One of the empty lines might even contain one or more <spaces>. I can not see it when I type, they do not add anything meaningful.

> After all, there had to be CR/LF if the line is only spaces, right?

Well, yes. Let's imagine the situation that there are 10 trailing CR/FL characters. The last blank line there contains 2 <space> characters. The code you propose will not remove anything. Every <space> character on one of the 'blank' lines will make your solution stop right there.

That may be intended, that is fine. It is up to the Asker to decide what he wants.


<end of meaningful text>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf>
<vbCrLf><space><space>
I want remove vbCrlf and spaces just from the end of textarea input