Home All Groups Group Topic Archive Search About

remove blank strings in arraylist

Author
19 Jan 2006 8:27 AM
James
below is some codes.

my arraylist below reads from a file.  My files contains blank line (ie
carriage return)

My message dialog shows all strings being captured. However i do not want my
array to contain "blank" string/line/carriage return

How do i remove the index so that my arraylist become smaller ?





****************************

Dim sr As StreamReader = New StreamReader(filespath)

Dim line As String

Do

line = sr.ReadLine

'add each line into arraylist

If InStr(line, ";") <> 1 Then

filestoexecute.Add(line)

End If

Loop Until line Is Nothing

sr.Close()

*****************************

For Each i In filestoexecute

MessageBox.Show(i)

Next

****************************

Author
19 Jan 2006 10:23 AM
Phill W.
"James" <jkk***@hotmail.com> wrote in message
news:eszU6GNHGHA.1628@TK2MSFTNGP12.phx.gbl...

> my arraylist below reads from a file.  My files contains blank line (ie
> carriage return)
.. . .
> How do i remove the index so that my arraylist become smaller ?

So you're trying to leave out blank lines and comments?

> If InStr(line, ";") <> 1 Then

If the line is blank, Instr() will return 0, which isn't 1, so will add the
line.
To exclude blank lines as well (if I can remember the "old" syntax for
this),
try this :

    If Len( line ) > 0 _
    AndAlso Instr( line, ";" ) <> 1 _
    Then

(BTW, using the methods on the String class, thios would be)

    If line.Length > 0 _
    AndAlso Not line.StartsWith( ";" ) _
    Then

HTH,
    Phill  W.
Author
19 Jan 2006 11:02 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"James" <jkk***@hotmail.com> schrieb:
> my arraylist below reads from a file.  My files contains blank line (ie
> carriage return)
>
> My message dialog shows all strings being captured. However i do not want
> my array to contain "blank" string/line/carriage return
>
> How do i remove the index so that my arraylist become smaller ?
>[...]
> Do
>
> line = sr.ReadLine
>
> 'add each line into arraylist
>
> If InStr(line, ";") <> 1 Then

\\\
If Len(line) > 0 AndAlso InStr(line, ";") <> 1 Then
    ...
End If
///

If you want to check the string with spaces on the right and left end of the
string, use 'Len(Trim(line)) > 0'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>