?
Solved

Delete extra blank line in textarea

Posted on 2009-02-22
18
Medium Priority
?
1,532 Views
Last Modified: 2012-05-06
How can I delete extra blank lines from the end of  textarea?
0
Comment
Question by:snow_Queen
  • 6
  • 3
  • 2
  • +4
18 Comments
 
LVL 4

Expert Comment

by:BasiKobrA
ID: 23704439
This is probably what you need :

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

Open in new window

0
 
LVL 4

Expert Comment

by:BasiKobrA
ID: 23704456
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.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23704689
"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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:snow_Queen
ID: 23704732
I need remove blank lines from the _END_ of textarea! Not all blank lines. Using ASP/VBScript platform.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23704761
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.
0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 23705529
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
0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 23705536
sorry.
That was for VB.NET
Maybe you can still use it.

As this:
Dim sChars
sChars As String = " "
YourString.TrimEnd(sChars)
0
 
LVL 12

Expert Comment

by:R_Harrison
ID: 23706518
Think this should work...
Do Until right(yourstring,2)<>vbcrlf
  yourstring=left(yourstring, len(yourstring)-2)
Loop

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 23706616
That will delete ALL of the "blank lines", including the first.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23706641
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
0
 
LVL 29

Expert Comment

by:Badotz
ID: 23706667
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
0
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 23706825
I'd use either a Split (see code snippet) or maybe a Regular expression. Because I consider also a line with just spaces a blank line.


<%
aTemp = Split(sText, vbCrLf)
For i = Ubound(aTemp) To 0 Step -1
    If Len(Trim(aTemp(i))) > 0 The Exit For
Next
ReDim Preserve aTemp(i)
sText = Join(aTemp, vbCrLf)
%>

Open in new window

0
 
LVL 28

Expert Comment

by:sybe
ID: 23706836
> 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
0
 
LVL 7

Expert Comment

by:Hecatonchires
ID: 23708030
Assuming you mean classic asp / vbscript

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

Open in new window

0
 
LVL 7

Expert Comment

by:Hecatonchires
ID: 23708033
left off a )  *slap*
mytextarea = rtrim(Request.Form("mytextarea"))

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 23708598
>>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?
0
 
LVL 28

Expert Comment

by:sybe
ID: 23708696
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>
0
 

Author Comment

by:snow_Queen
ID: 23709789
I want remove vbCrlf and spaces just from the end of textarea input
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question